繁体   English   中英

R用NA替换textConnection中缺少的元素

[英]R Replace missing elements in a textConnection with NA

如果我做

a = read.table(textConnection('a b c d
1 2 3 4
a b c
1 2 3 4','r'),header=T)

我收到一个错误

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 3 did not have 4 elements

如何让R用NA替换丢失的元素而不是失败?

使用fill=TRUE

a = read.table(textConnection('a b c d
1 2 3 4
a b c
1 2 3 4','r'),header=T, fill=TRUE)

a
##   a b c  d
## 1 1 2 3  4
## 2 a b c NA
## 3 1 2 3  4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM