mapply over two lists
11,649
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
Author by
user1322296
Updated on June 24, 2022Comments
-
user1322296 almost 2 years
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) inmylist1
and some vectors from the first element (a dataframe) inmylist2
and regress them against each other. Then move onto the nextmylist1
element andmylist2
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]))