我不知道如何使用colClasses
中的fread
选项选择特定列。我试图以几种方式使用NULL
,但没有任何效果。这是一个最小的例子。我只想要第1列和第3列。
dt <- data.table(a=1:5,b=6:10,c=10:14)
write.csv(dt,"dt.csv",row.names=F)
dt <- fread("dt.csv",colClasses=?)
packageVersion("data.table")
[1] ‘1.8.10’
getRversion()
[1] ‘3.0.1’
导入的数据集应如下所示:
a c
1: 1 10
2: 2 11
3: 3 12
4: 4 13
5: 5 14
答案 0 :(得分:42)
更新:自提交966起,现在在R-Forge的v1.8.11中实施。来自NEWS:
fread
drop
,select
和NULL
colClasses
已实施。至 按名称或按数字删除或选择列。请参阅?fread
中的示例。
?fread
中的示例是:
data = "A,B,C,D\n1,3,5,7\n2,4,6,8\n"
# colClasses
fread(data, colClasses=c(B="character",C="character",D="character"))
fread(data, colClasses=list(character=c("B","C","D"))) # saves typing
fread(data, colClasses=list(character=2:4)) # same using column numbers
# drop
fread(data, colClasses=c("B"="NULL","C"="NULL")) # as read.csv
fread(data, colClasses=list(NULL=c("B","C"))) # same
fread(data, drop=c("B","C")) # same but less typing, easier to read
fread(data, drop=2:3) # same using column numbers
# select
# (in read.csv you need to work out which to drop)
fread(data, select=c("A","D")) # less typing, easier to read
fread(data, select=c(1,4)) # same using column numbers