Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串列表转换为R中的数字数据帧_R_List_Dataframe_Mapply - Fatal编程技术网

将字符串列表转换为R中的数字数据帧

将字符串列表转换为R中的数字数据帧,r,list,dataframe,mapply,R,List,Dataframe,Mapply,我有一个字符串列表,如下所示: > ll [[1]] [1] "2" "1" [[2]] character(0) [[3]] [1] "1" [[4]] [1] "1" "8" 最长的列表的长度为2,我想从这个列表中构建一个包含2列的数据框。还可以将列表中的每个项目转换为数字或字符(0)的NA,从而获得额外点数。我已经尝试使用mappy()和data.frame将其转换为数据帧,并使用NA填充,如下所示 # Find length of each list element l

我有一个字符串列表,如下所示:

> ll

[[1]]
[1] "2" "1"

[[2]]
character(0)

[[3]]
[1] "1"

[[4]]
[1] "1" "8"
最长的列表的长度为2,我想从这个列表中构建一个包含2列的数据框。还可以将列表中的每个项目转换为数字或字符(0)的NA,从而获得额外点数。我已经尝试使用mappy()和data.frame将其转换为数据帧,并使用NA填充,如下所示

#  Find length of each list element
len = sapply(awards2, length)

#  Number of NAs to fill for column shorter than longest
len = 2 - len

df = data.frame(mapply( function(x,y) c( x , rep( NA , y ) ) , ll , len))
但是,我没有使用上面的代码得到一个包含2列的数据帧(NA作为填充)


谢谢您的帮助。

我们可以从
stringi
使用
stri\u list2matrix
。由于
列表
元素都是
字符
向量,因此使用此函数似乎没有问题

library(stringi)
t(stri_list2matrix(ll))
#     [,1] [,2]
#[1,] "2"  "1" 
#[2,] NA   NA  
#[3,] "1"  NA  
#[4,] "1"  "8" 

如果我们需要转换为
data.frame
,请使用
as.data.frame

sapply(ll,[',1:max(length(ll)))
@d.b将其包装起来,列数是已知的,因此您可以使用
length再次感谢Akrun!我想我需要学习您使用的这些字符串包。