Questions tagged [mapply]

R function that applies a function to multiple lists or vector arguments.

R function mapply is a multivariate version of sapply. mapply applies FUN argument to the first elements of each argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.

547 questions
3
votes
1 answer

Naming / Names of the apply function family in R

I'm currently studying R. In particular I need to remember the functions in the apply-function family (e.g. lapply, sapply, mapply, etc.). I know what each function in the apply-function family does (especially this answer helps a lot), but mixes…
Herickson
  • 133
  • 4
3
votes
0 answers

Why does mapply not store ggplot objects in the output

When we use mapply in this scenario it will return the actual numbers that resulted from multiplying x and y: mult.fun <- function(x, y) { x*y } res <- mapply(mult.fun, seq(1,3), seq(1,3)) # gives > 1 4 9 Then we can access the results later on…
CodeNoob
  • 1,988
  • 1
  • 11
  • 33
3
votes
2 answers

multiply two lists of irregular length

I have lists like a <- list(list(c(-2,1), 4:5, 2:3), list(c(0,2), c(-1,1))) b <- list(7:9, c(5,-1)) > a [[1]] [[1]][[1]] [1] -2 1 [[1]][[2]] [1] 4 5 [[1]][[3]] [1] 2 3 [[2]] [[2]][[1]] [1] 0 2 [[2]][[2]] [1] -1 1 > b [[1]] [1] 7 8…
Christoph Hanck
  • 353
  • 4
  • 14
3
votes
2 answers

Multiple list nesting with split(), R

Given a dataset with multiple unique elements in a column, I'd like to split those unique elements into new dataframes, but have the dataframe nested one level down. Essentially adding an extra level to the split() command. For instance (using…
moxed
  • 343
  • 1
  • 6
  • 16
3
votes
2 answers

Rename Columns of dataframe based on names of list in R

I have multiple dataframes saved in a list object. They share the same two column names. I'd like to rename the second column to the name of the dataframe. Example Data: df1 <- data.frame(A = 1:10, B= 11:20) df2 <- data.frame(A = 21:30, B = 31:40)…
user6883405
  • 393
  • 3
  • 14
3
votes
3 answers

R: merge two lists of lists of dataframes

I have two lists of lists of dataframes like this: L1 <- list(Q1=list(A=data.frame(X1=1:3),C=data.frame(X1=1:3)), Q2=list(B=data.frame(X1=1:3),C=data.frame(X1=1:3))) L2 <- list(Q1=list(B=data.frame(X1=4:6),C=data.frame(X1=4:6)), …
rosapluesch
  • 250
  • 2
  • 10
3
votes
2 answers

Progress bar and mapply (input as list)

I would like to monitor the progress of my mapply function. The data consists of 2 lists and there is a function with 2 arguments. If I do something similar with a function that takes 1 arguments I can use ldply instead of lapply. (I'd like to…
Roccer
  • 899
  • 2
  • 10
  • 25
3
votes
1 answer

Struggling with Mapply

So I understand that mapply will step through an array (or series of arrays) in element order – I was wondering if there was a way to STOP it from doing this on some of the arguments…. Let me explain further I have a set of data points, with X and Y…
3
votes
1 answer

How to replicate an Exact Binomial Test in R so that it runs through entire columns in a dataframe and generates an output

binom.test(4175, 6534, p = 0.5, + alternative = c("two.sided"), + conf.level = 0.95) Exact binomial test #Output data: 4175 and 6534 number of successes = 4175, number of trials = 6534, p-value < 2.2e-16 …
Alus
  • 33
  • 3
3
votes
2 answers

Error when using dplyr::mutate with mapply

I want to use mutate on each of two data frames in a list, adding a column z = 3 to the first and z = 4 to the second (and returning a list of two data frames). dfs <- list(data.frame(x = 1), data.frame(y = 2)) mapply(dplyr::mutate, dfs, z = 3:4,…
Rory Nolan
  • 972
  • 10
  • 15
3
votes
2 answers

extract elements from a list based on a vector of indices

I want to extract elements from a list based on indices stored in a separate vector. This is my attempt at it: list_positions<-c(2,3,4) my_list<-list(c(1,3,4),c(2,3,4,5,6),c(1,2,3,4,6)) my_fun<-function(x,y){ …
Vitalijs
  • 938
  • 7
  • 18
3
votes
2 answers

preserve name of list element when saving using mapply

I'm using mapply() to save elements from a list in separate files. E.g. file.names <- c('~/a.RData', '~/b.RData') data.list <- list(foo = c(1:10), bar = rep(1, 10)) mapply(function(x, y) save(x, file = y), data.list, file.names) and I'd like to…
Lukas
  • 655
  • 8
  • 20
3
votes
0 answers

R: merging lists of data frames on multiple keys

I'm trying to simultaneously merge several data frames in r by combining them into lists and using mapply and merge. This works fine when merging on a single key, but not when merging on multiple keys using the 'by = c("a","b")' argument in merge.…
John Clegg
  • 99
  • 8
3
votes
1 answer

Apply multiple functions to two dataframes column-wise

Given the following sample data: library(Metrics) obs=data.frame(replicate(10,runif(100))) pred=data.frame(replicate(10,runif(100))) obs1=as.data.frame(lapply(obs, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc),…
code123
  • 2,082
  • 4
  • 30
  • 53
3
votes
1 answer

Shifting rows of matrices depending on some other data

I'm sorry for repeating a question about the *apply functions, but I cannot get my code to work with the material that I found so far. I have a matrix (stored in a large data frame) and I want to shift the rows of this matrix by a certain amount (to…