Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

r - Force mapply to return a list?

Suppose I have a function that creates data frames. I'd like to run that function with different input values, and then rbind the results together into one big data frame, as below:

CreateDataFrame <- function(type="A", n=10, n.true=8) {
  data.frame(success=c(rep(TRUE, n.true), rep(FALSE, n - n.true)), type=type)
}
df <- do.call(rbind, lapply(toupper(letters[1:5]), CreateDataFrame))

My CreateDataFrame function takes three arguments. In the example above, the second and third arguments are held constant. I'd like to do the same as above, but have the second and third arguments change on each call. I think I have to use mapply, like this:

mapply("CreateDataFrame", type=toupper(letters[1:5]), n=10, n.true=8:4)

I'm having trouble because mapply isn't returning a list, which prevents me from running do.call(rbind, mapply(...)). How can I end up with a single data frame, as I did in the example at the top?

Looks like mapply is returning a matrix of lists. I was expecting it to return a list of data frames. What should I do differently?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get a list of data.frames as the return value, set mapply's SIMPLIFY argument to FALSE. (Its default value is TRUE, which directs the function to "attempt to reduce the result to a vector, matrix or higher dimensional array" -- just what you experienced).

res <- mapply("CreateDataFrame", type=toupper(letters[1:5]), n=10, n.true=8:4, 
              SIMPLIFY = FALSE)

identical(class(res), "list")
[1] TRUE

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...