2014-01-16 56 views
3

我想將一個字符串列表轉換爲數據框。 不過,我得到這個錯誤:R textConnection:「參數」對象「必須deparse到一個單一的字符串」

> read.csv(textConnection(c("id,name,count", 
          '6289,aa,16', 
          '6269,bb,8', 
          '6269,cc,8', 
          '6269,dd,8', 
          '6610,ee,4'))) 
Error in textConnection(c("id,name,count", "6289,aa,16", "6269,bb,8", : 
    argument 'object' must deparse to a single character string 
Calls: read.csv -> read.table -> textConnection 

當我刪除只是一條線,它的工作原理:

> read.csv(textConnection(c("id,name,count", 
          '6289,aa,16', 
          '6269,bb,8', 
          '6269,cc,8', 
          '6610,ee,4'))) 
    id name count 
1 6289 aa 16 
2 6269 bb  8 
3 6269 cc  8 
4 6610 ee  4 

是怎麼回事?

+1

Wierd。如果你做'txt < - c('id,name,count'...); read.csv(textConnection(txt))'它工作得很好。奇怪。 –

+1

這似乎是'deparse'和'width.cutoff'值設置爲60的問題。這會導致長度超過1的字符串。 – A5C1D2H2I1M1N2O1R2T1

+0

另外,只是好奇:爲了你說明的目標,爲什麼你使用'textConnection'而不是'read'table'和family的'text'參數? – A5C1D2H2I1M1N2O1R2T1

回答

2

Ananda對此行爲起源於deparse。根據textConnection()文件的詳細信息部分,它說,

object should be the name of a character vector: however, short expressions will be accepted provided they deparse to less than 60 bytes.

deparse()變成表達成字符串。當表達式解析爲多於60個字節時,deparse會嘗試將字符串拆分爲更短的文本塊;即一串字符串。試試看:

deparsed <- deparse(c("this expr","deparses to length=1,","nchar=57 bytes")) 
deparsed 
[1] "c(\"this expr\", \"deparses to length=1,\", \"nchar=57 bytes\")" 
nchar(deparsed) 
[1] 57 

deparsed <- deparse(c("whereas this longer expression","deparses to length=2,","nchar=c(61,23) bytes")) 
deparsed 
[1] "c(\"whereas this longer expression\", \"deparses to length=2,\", " 
[2] "\"nchar=c(61,23) bytes\")" 
nchar(deparsed) 
[1] 61 23 

這就是爲什麼你得到錯誤

argument 'object' must deparse to a single character string 

您不再表達,而不是你的短之一。

正如sds和Simon所示,解決方案是將您的表達式分配給對象,然後在對象名稱而不是原始表達式上調用textConnection。

txt <- c("id,name,count", '6289,aa,16', '6269,bb,8', 
     '6269,cc,8', '6269,dd,8', '6610,ee,4') 
read.csv(textConnection(txt)) 
    id name count 
1 6289 aa 16 
2 6269 bb  8 
3 6269 cc  8 
4 6269 dd  8 
5 6610 ee  4 

引擎蓋下,textConnection現在要求deparse(substitute(txt))而非deparse(substitute(c("id,name,count", ...)))。只有較短的表達式纔會解析爲單個字符串。這就是textConnection需要的。

+0

比我所給出的更好的解釋。 +1 – A5C1D2H2I1M1N2O1R2T1

相關問題