I am reading a txt file with data.table package.
df<- fread("df.txt")
head(df)
Number Region Type Car ...
1 1 1
2 1 2
3 1 1
4 1 1
5 2 2
6 2 3
I would like to do a subset of df with the Type Car iqual to 1 and 3. When I write something like this
>class(df)
"data.table" "data.frame"
>subset(df, Type Car %in% c(1,3))
This does not work. Some solution?
You've got a data table from fread()
(unless you used data.table = FALSE
), so you can use data table row subsetting instead of subset()
. Since you have a multi-word column name, you will need to apply back-ticks around it.
df[`Type Car` %in% c(1, 3)]
The same goes for subset()
if you choose to use it. In fact, back-ticks will always be necessary when referencing multi-word names that contain spaces. It would be better to use qualified R names. You can reset the names with
setnames(df, make.names(names(df), unique = TRUE))
so you can avoid the back-ticks. Then you could do
df[Type.Car %in% c(1, 3)]
Note: In data.table version 1.9.6, you can now name the columns in fread()
with the col.names
argument. As Michael Chirico has mentioned, it's best to get this problem out of the way immediately.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.