我的数据存储在一个如下所示的文本文件中:
1, { {0, 1}, {1, 0}, {2, 6} }
2, { {0, 3}, {2, 2}, {0, 1} }
...
第一个元素是整数,第二个元素是二维数组。是否有一个函数将其读入R?
答案 0 :(得分:4)
data.frame( lapply( read.csv(text=
"1, { {0, 1}, {1, 0}, {2, 6} } # will read the line as mostly character columns
2, { {0, 3}, {2, 2}, {0, 1} } # with '{' and '}' just as non-syntactic characters
", header=FALSE ),
function(x) as.numeric( gsub("[^[:digit:]]", "", x) )
) )
#----------------------
V1 V2 V3 V4 V5 V6 V7
1 1 0 1 1 0 2 6
2 2 0 3 2 2 0 1