[英]R: using lapply with data frames and custom function
I have a question about using lapply
with lists of dataframes. 我有一个关于将lapply
与数据帧列表一起使用的问题。 Suppose I have two lists: 假设我有两个列表:
list1<-list(data1,data2)
list2<-list(data3,data4)
I want to write a function that appends a column and row from one dataframe to another. 我想编写一个函数,将一个数据帧的列和行追加到另一个数据帧。 This is my function: 这是我的功能:
append<-function(origin.data, destin.data){
vec.to.append<-origin.data[,1]
#add as the first column
destin.data<-cbind(vec.to.append, destin.data)
#add as first row
vec.to.append<-t(c(0, vec.to.append))
destin.data<-rbind(vec.to.append, destin.data)
return(destin.data)
}
This works fine if I run 如果我跑步,这会很好
append(list1[[1]], list2[[1]])
or 要么
append(list1[[2]], list2[[2]])
but gives me an error when I try to use lapply
: 但是当我尝试使用lapply
时给了我一个错误:
trial<-lapply(list1, append, destin.data=list2)
The error looks straightforward: 该错误看起来很简单:
Error in rbind(vec.to.append, destin.data) : number of columns of matrices must match (see arg 2)
but as I checked the dimensions of my dataframes and vector, all looked fine. 但是当我检查数据框和向量的尺寸时,一切看起来都很好。 Plus, the function runs and gives the expected result if I do not use lapply
but do it "argument by argument". 另外,如果我不使用lapply
而是“逐个参数”地运行,该函数将运行并给出预期的结果。 Can somebody help me out? 有人可以帮我吗?
By the way, I saw the answer here: Using lapply with changing arguments but applying my function over the list names gives an empty result: 顺便说一句,我在这里看到了答案: 对变化的参数使用lapply,但对列表名称应用我的函数将得到一个空结果:
prova<-lapply(names(list1), append, destin.data=list2)
prova
list()
Clearly, I am missing something! 显然,我缺少了一些东西!
lapply(list1, append, destin.data=list2)
is equivalent to: lapply(list1, append, destin.data=list2)
等效于:
list(append(list1[[1]], list2),
append(list1[[2]], list2)
Which is not what you want. 这不是您想要的。 If you want to pair up list1 and list2, element-wise, you should do: 如果要按元素方式将list1和list2配对,则应该执行以下操作:
lapply(seq_along(list1), function(i) append(list1[[i]], list2[[i]])
This is equivalent to: 这等效于:
list(append(list1[[1]], list2[[1]]),
append(list1[[2]], list2[[2]])
which is what you do want, right? 您想要的是什么,对吗?
edit: as the other answer says, you can also use mapply
, which is a little tidier, but this is the lapply
-based solution. 编辑:正如另一个答案所说,您也可以使用mapply
,这有点小,但这是基于lapply
的解决方案。 Result is the same in either case. 在两种情况下结果都是相同的。
lapply
only iterates over one list. lapply
仅迭代一个列表。 The last argument you pass is passed on to the function as-is (ie the function append
gets passed all of list2
rather than just one element). 您传递的最后一个参数按原样传递给函数(即,函数append
传递了整个list2
而不是仅传递一个元素)。
You can use Map
or mapply
instead. 您可以改用Map
或mapply
。 Note that the order of the arguments here is reversed in comparison to lapply
: 请注意,此处的参数顺序与lapply
相比是相反的:
result = Map(append, list1, list2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.