Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/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
为什么rbind()和do.call(rbind,)返回不同的结果?_R_Rbind_Do.call - Fatal编程技术网

为什么rbind()和do.call(rbind,)返回不同的结果?

为什么rbind()和do.call(rbind,)返回不同的结果?,r,rbind,do.call,R,Rbind,Do.call,我想使用以下代码将列表转换为数据帧: ls<-list(a=c(1:4),b=c(3:6)) do.call("rbind",ls) 但是,如果我直接使用rbind,它将返回一个列表 为什么rbind在这两种情况下表现不同? my.df<-rbind(ls) str(ls) my.df a b ls Integer,4 Integer,4 str(ls) List of 2 $ a: int [1:4] 1 2 3 4 $ b:

我想使用以下代码将列表转换为数据帧:

ls<-list(a=c(1:4),b=c(3:6))
do.call("rbind",ls)
但是,如果我直接使用
rbind
,它将返回一个列表

为什么
rbind
在这两种情况下表现不同?

my.df<-rbind(ls)
str(ls)


 my.df
   a         b        
ls Integer,4 Integer,4

 str(ls)
List of 2
 $ a: int [1:4] 1 2 3 4
 $ b: int [1:4] 3 4 5 6
my.df
do.call(rbind,ls)
提供与
Reduce(rbind,ls)
相同的输出。后者效率较低,但它用于显示如何在
ls
中迭代对象,而不是直接操作
ls
(这是两个列表的串联列表)


它们都是通过“取消列出”列表中的每个元素来操作的,该元素具有class
numeric
。当您使用
rbind
数值参数时,生成的类是一个矩阵,
typeof
为整数。如果您只是
rbind
列表,那么列表中的每个元素都被视为一个对象。因此,返回的对象是一个
矩阵
对象,具有1行2列和
列表
类型的条目。如果它有一行,那么它显然将对象
ls
视为一件事,而不是两件事。键入rbind(ls,ls,ls)
将给出3行2列。

do.call(“rbind,ls)
返回一个矩阵,而不是数据帧。与
rbind(ls)
相同。因为
do.call
从列表中提取元素-这就是区别。就像你会做
rbind(ls[[1]],ls[[2]])
谢谢@RichardScriven,但是
rbind(ls)
的输出仍然是一个列表。实际上我想要的是将一个列表转换成一个矩阵否,
rbind(ls)
的输出是一个矩阵。它包含列表元素。请参见
class(rbind(ls))
,因为在提出此类问题之前,您应该阅读函数的帮助页面<代码>?拨打电话
my.df<-rbind(ls)
str(ls)


 my.df
   a         b        
ls Integer,4 Integer,4

 str(ls)
List of 2
 $ a: int [1:4] 1 2 3 4
 $ b: int [1:4] 3 4 5 6