1

如果我做

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 替换缺失的元素而不是因错误而失败?

4

1 回答 1

5

使用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
于 2013-05-29T03:28:25.810 回答