ogriofac ogriofac - 3 days ago 6
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

VAL
variable is equal to 24.

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

and read it to R using
read.table
:

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
[.data.frame
(DT, , .N, by = VAL) : unused argument (by = VAL)


DT[, .N]

returns:


data frame with 0 columns and 6496 rows


However, when I run
head(DT)
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?

Answer

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

nrow(DT[DT$VAL==24,])

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

library(data.table)
setDT(DT)
DT[,.(Count = .N),by = .(VAL)]
Comments