Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapply over two lists [closed]

Tags:

list

r

apply

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]))  
like image 573
user1322296 Avatar asked Dec 12 '25 20:12

user1322296


1 Answers

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

like image 99
shhhhimhuntingrabbits Avatar answered Dec 14 '25 12:12

shhhhimhuntingrabbits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!