ogriofac ogriofac - 7 months ago 49
R Question

R: .N in data.table returning 0 columns

I have searched and can't find a similar question. I'm trying to count the rows in a data.frame in which the value of the

variable is equal to 24.

I downloaded the data from https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv

and read it to R using

download.file(fileUrl, destfile = "./Housing_Data/Housingdata.csv", method = "curl")
DT <- read.table("./Housing_Data/Housingdata.csv", sep = ",", header = TRUE)

I tried

DT[, .N, by=VAL]

which returned:

Error in
(DT, , .N, by = VAL) : unused argument (by = VAL)

DT[, .N]


data frame with 0 columns and 6496 rows

However, when I run
it returns as if the columns are loaded correctly.

I'm really not sure where I'm going wrong here, can anyone point me in the right direction?


Looks like you're trying to use data.table operations on a data.frame. And your syntax looks a little off for the data.table as well.

This is how you would find the nrow where VAL == 24


If you want to do this with a data.table you'll first have to a data.table. Run this:

DT[,.(Count = .N),by = .(VAL)]