Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

r - using colClasses in fread

I don't know how to choose specific columns using the colClasses option in fread. I tried to use NULL in several ways but nothing worked. Here's a minimal example. I just want columns 1 and 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’

The imported dataset should look like this:

   a  c
1: 1 10
2: 2 11
3: 3 12
4: 4 13
5: 5 14
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATE: This is now implemented in v1.8.11 on R-Forge as of commit 966. From NEWS :

fread's drop, select and NULL in colClasses are implemented. To drop or select columns by name or by number. See examples in ?fread.

The examples in ?fread are :

data = "A,B,C,D
1,3,5,7
2,4,6,8
"

# 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...