只读取选定列有谁能告诉我如何只读取以下数据的前6个月(7列),例如使用read.table()?Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 2009 -41 -27 -25 -31 -31 -39 -25 -15 -30 -27 -21 -252010 -41 -27 -25 -31 -31 -39 -25 -15 -30 -27 -21 -25 2011 -21 -27 -2 -6 -10 -32 -13 -12 -27 -30 -38 -29
3 回答
![?](http://img1.sycdn.imooc.com/545862e700016daa02200220-100-100.jpg)
至尊宝的传说
TA贡献1789条经验 获得超10个赞
data.txt
colClasses
read.table()
"integer"
"NULL"
> read.table("data.txt", colClasses = c(rep("integer", 7), rep("NULL", 6)), + header = TRUE) Year Jan Feb Mar Apr May Jun1 2009 -41 -27 -25 -31 -31 -392 2010 -41 -27 -25 -31 -31 -393 2011 -21 -27 -2 -6 -10 -32
"integer"
?read.table
data.txt
$ cat data.txt "Year" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"2009 -41 -27 -25 -31 -31 -39 -25 -15 -30 -27 -21 -252010 -41 -27 -25 -31 -31 -39 -25 -15 -30 -27 -21 -252011 -21 -27 -2 -6 -10 -32 -13 -12 -27 -30 -38 -29
write.table(dat, file = "data.txt", row.names = FALSE)
dat
dat <- structure(list(Year = 2009:2011, Jan = c(-41L, -41L, -21L), Feb = c(-27L, -27L, -27L), Mar = c(-25L, -25L, -2L), Apr = c(-31L, -31L, -6L), May = c(-31L, -31L, -10L), Jun = c(-39L, -39L, -32L), Jul = c(-25L, -25L, -13L), Aug = c(-15L, -15L, -12L), Sep = c(-30L, -30L, -27L), Oct = c(-27L, -27L, -30L), Nov = c(-21L, -21L, -38L), Dec = c(-25L, -25L, -29L)), .Names = c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = "data.frame",row.names = c(NA, -3L))
count.fields
## returns a vector equal to the number of lines in the filecount.fields("data.txt", sep = "\t")## returns the maximum to set colClassesmax(count.fields("data.txt", sep = "\t"))
- 3 回答
- 0 关注
- 538 浏览
添加回答
举报
0/150
提交
取消