mapply over two lists [closed]

混江龙づ霸主 提交于 2019-12-04 04:20:58

问题


I recentely asked a question about using an apply function over two lists. Each list is a list of data frames created by splitting a large dataframe. For each time the function runs I want to take vectors from the first element (a dataframe) in mylist1 and some vectors from the first element (a dataframe) in mylist2 and regress them against each other. Then move onto the next mylist1 element and mylist2 element. Effectively the function takes two lists with the same number of elements and takes a pair (one from each list) and plays about with them.

I tried the following, but the results I get are not what I want:

a1<-c(1:5,rep(0,5))
a2<-c(1:5,10:6)
b2<-c(rep(100,5),rep(50,5))
z<-c(rep("part1",5),rep("part2",5))
df1<-data.frame(a1,z)
df2<-data.frame(a2,b2,z)

mylist1<-split(df1,z)
mylist2<-split(df2,z)


myfunction<-function(x,y) 
{

meana <- mean(x$a)
meanb <- mean(y$b)
model<-lm((x$a)~(y$a))
return(c(model$coefficients[2],meana=meana,meanb=meanb))
}

result <- mapply(myfunction,x=mylist,y=mylist2)

 #result
#        x   y
#y$a     1  -1
#meana   3   8
#meanb 100  50

What I want is:

#y$a     1   0   
#meana   3   0
#meanb   100 50


#e.g. the results in the first row are from lm((mylist1[[1]][,1])~(mylist2[[1]][,1]))  and lm((mylist1[[2]][,1])~(mylist2[[2]][,1]))  

回答1:


I ran your code and got

> result <- mapply(myfunction,x=mylist1,y=mylist2)
> result
      part1 part2
y$a       1     0
meana     3     0
meanb   100    50

you have a typo

result <- mapply(myfunction,x=mylist,y=mylist2)

which I changed to

result <- mapply(myfunction,x=mylist1,y=mylist2)

maybe this is the issue



来源:https://stackoverflow.com/questions/11062540/mapply-over-two-lists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!