## 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 `i`

th 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 `lapply`

s 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

Why (Or When) Is Rscript (Or Littler) Better Than R Cmd Batch

Ggplot: Adding Regression Line Equation and R2 with Facet

Ggplot: Boxplot of Multiple Column Values

R: Sample() Command Subject to a Constraint

Bars in Geom_Bar Have Unwanted Different Widths When Using Facet_Wrap

Count Number of Non-Na Values for Every Column in a Dataframe

How to Load Data Quickly into R

One Function to Detect Nan, Na, Inf, -Inf, etc.

Differencebetween Nan and Inf, and Null and Na in R

Importing a Big Xlsx File into R

Changing Font in PDF Produced by Rmarkdown

Obtaining Threshold Values from a Roc Curve

Left Join Only Selected Columns in R with the Merge() Function

Count Number of Records and Generate Row Number Within Each Group in a Data.Table