问题
I do not use that many functions but when I do I tend to use an anon function and some form of apply
. I now however am trying to write a function that works over items in a list.
There are two lists that each have many items (by item I mean e.g. mylist1[1]
). All items are dataframes. I want to take the first dataframe from mylist1
and the first dataframe from mylist2
and run a bunch of functions over the columns in those dataframes. Then take the 2nd mylist1
item and the 2nd mylist2
item and so on...
Below is the sort of thing I am used to writing but clearly does not work in this case with two lists. Can anyone help me out with a fast way to figure out how I should approach this using something other than sapply
method that seems to be causing the main problem.
a <- c(1:10)
b <- c(1:10)
z <- c(rep("x", 5), rep("y", 5))
df <- data.frame(cbind(a, b, z))
mylist1 <- split(df, z)
mylist2 <- split(df, z)
myfunction <- function(x, y)
{
a <- as.data.frame(x[1])
b <- as.data.frame(y[1])
meana <- mean(a[1])
meanb <- mean(b[1])
model <- lm(a[1]~b[1])
return(c(model$coefficients[2], meana, meanb))
}
result <- sapply(mylist1, mylist2, myfunction)
I also just thought do people think it would be better to subset
by z
rather than split
and do the function that way?
回答1:
You are describing exactly the use case for mapply
.
result <- mapply(myfunction,x=mylist,y=mylist2)
Unfortunately your example doesn't seem to enjoy being passed two data.frames (x, y 's first elements are both data.frames, which x[1]
and y[1]
would seem to contradict).
来源:https://stackoverflow.com/questions/11051087/function-over-more-than-one-list