簡體   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