Force Mapply to Return a List

Force mapply to return a list?

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

Force apply to return a list

You have to split matrix mat before applying function f.

list_result <- lapply(split(mat,seq(NROW(mat))),f)
matrix_result <- do.call(rbind,list_result)

Return multiple lists in mapply

How about:

Use mapply but don't simplify.

result1 <- mapply(func, x=df, SIMPLIFY=FALSE)

Iterate over the indices (this assumes both lists in the result are the same length); for each index i, use lapply to pull out the ith element of each list in result1.

 result2 <- lapply(seq_along(result1[[1]]),
function(i) lapply(result1,"[[",i))

I tried a little bit to shorten/obfuscate this still further (i.e. remove the need to define an anonymous function), but with nested lapplys I can't quite figure out how to make it work.

mapply for a single list

1) Use do.call :

do.call("mapply", c(pasteNotNA, inp_list, USE.NAMES = FALSE))
## [1] "a, X, 1" "Y, 2" "c, Z"

2) or use pmap_chr in the purrr package:

library(purrr)

pmap_chr(inp_list, pasteNotNA)
## [1] "a, X, 1" "Y, 2" "c, Z"

3) This also works:

apply(as.data.frame(inp_list), 1, pasteNotNA)
## [1] "a, X, 1" "Y, 2" "c, Z"

mapply not having same return datatype

Why your example is not working? vector in R could contains only one data type. When a few types is provided automatic conversion is applied always to lower level representation - here to a character. A list have to be used to grab different data types.
Solution with Map

f <- function(a){
if(a>10)
return('any string')
else{
return(a)
}
}

Map(f, c(1,20,10))

R: mapply outputs matrix instead of function output

By default, SIMPLIFY = TRUE which outputs a matrix. Try:

mapply(plot.volcano.norm, data.list, title.list, SIMPLIFY = FALSE)

mapply with list and vector

Try this:

res = mapply(myFun, myList, myVec, SIMPLIFY = FALSE)

Progress bar and mapply (input as list)

Answering my own question.
There is now a package that can do that. It is called pbapply. The function I was looking for is pbmapply.

Using mapply to select from elements from a nested list using multiple arguments

Try this. It is better to use a function to catch the desired values. The reason why you got an error is because functions works different when using indexing. It is better to set the function directly inside the *apply() sketch to reach the desired outcome. Here the code:

#Code
unlist(mapply(function(x,y) x[y],x=list1,y=list2))

Output:

[1]  4  8  5  8 15 17 12 15  3 15

Or if you want the output in a list:

#Code 2
List <- mapply(function(x,y) x[y],x=list1,y=list2)

Output:

List
[[1]]
[1] 4

[[2]]
[1] 8

[[3]]
[1] 5

[[4]]
[1] 8

[[5]]
[1] 15

[[6]]
[1] 17

[[7]]
[1] 12

[[8]]
[1] 15

[[9]]
[1] 3

[[10]]
[1] 15

Another simplified options can be (Many thanks and all credit to @27ϕ9):

#Code3
mapply(`[[`, list1, list2)

Output:

[1]  4  8  5  8 15 17 12 15  3 15

Or:

#Code4
mapply(`[`, list1, list2)

Output:

[[1]]
[1] 4

[[2]]
[1] 8

[[3]]
[1] 5

[[4]]
[1] 8

[[5]]
[1] 15

[[6]]
[1] 17

[[7]]
[1] 12

[[8]]
[1] 15

[[9]]
[1] 3

[[10]]
[1] 15

R use mapply on nested list

A double loop Map/mapply will do what the question asks for.

Map(function(i) mapply(`[`, mylist[[i]], not_a[[i]], SIMPLIFY = FALSE), seq_along(mylist))

Simpler:

Map(function(x, y) Map(`[`, x, y), mylist, not_a)


Related Topics



Leave a reply



Submit