将二维数组读入R中

时间:2012-06-04 23:13:30

标签: r

我的数据存储在一个如下所示的文本文件中:

1, { {0, 1}, {1, 0}, {2, 6} }

2, { {0, 3}, {2, 2}, {0, 1} }
...

第一个元素是整数,第二个元素是二维数组。是否有一个函数将其读入R?

1 个答案:

答案 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