Importing a txt file when number of columns varies?

若如初见. 提交于 2019-12-01 04:46:44

You need to use the fill argument in the read.table function. Suppose I have the following file

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

called tmp.txt. Note that row two has only two values. Then

> a = read.table("tmp.txt", sep=",", header=TRUE, fill=TRUE)
> a
  A B  C 
1 1 2  3
2 4 5 NA
3 6 7  8

You use then standard sub-setting commands to remove (if you want to), any rows that contain NA:

> a[!is.na(a$C),]
  A B  C 
1 1 2  3
3 6 7  8
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!