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
4
votes
2 answers

Parallelize user-defined function using apply family in R

I have a script that takes too long to compute and I'm trying to paralellize its execution. The script basically loops through each row of a data frame and perform some calculations as shown below: my.df = data.frame(id=1:9,value=11:19) sumPrevious…
Victor
  • 1,163
  • 4
  • 25
  • 45
4
votes
2 answers

How to combine rapply() and mapply(), or how to use mapply/Map recursively?

I was wondering if there's a simple way to combine the functions of rapply( , how = "replace") and mapply(), in order to use mapply() on nested lists recursively. For instance, I have two nested lists: A = list(list(c(1,2,3), c(2,3,4)),…
shenglih
  • 879
  • 2
  • 8
  • 18
4
votes
2 answers

How to specify which arguments to be iterated in mapply in R

I am new to R, hence I am new to the apply functions. I haven't found the answer to this question anywhere, even though I have a (not so elegant) way of solving it. Consider this dummy code: my.fun <- function(vector1, vector2, vector3 = NULL) { …
jose
  • 220
  • 1
  • 12
4
votes
2 answers

Using mapply with mean function on a matrix

I wish to calculate the mean of adjacent values in each column (or row) of a matrix (e.g. mean of [1,1] and [2,1], [2,1] and [3,1], [3,1] and [4,1]) and to apply this across all columns. I have tried to use the mapply function (to avoid using a for…
PDN
  • 43
  • 3
4
votes
1 answer

Applying some functions to multiple objects

I am on Mac OS 10.10 with R 3.1.1 Suppose I have the following data frames a and b with the same attributes: a<- structure(list(X1 = 1:5, X2 = 6:10), .Names = c("X1", "X2"), row.names = c(NA, -5L), class = "data.frame") b<- structure(list(X1 =…
mallet
  • 2,454
  • 3
  • 37
  • 64
4
votes
3 answers

R - Apply function with different argument value for each row/column of a matrix

I am trying to apply a function to each row or column of a matrix, but I need to pass a different argument value for each row. I thought I was familiar with lapply, mapply etc... But probably not enough. As a simple example : >…
RFen
  • 141
  • 1
  • 7
4
votes
3 answers

How to get sum of the list using mapply in r

I have a list of 100 elements, and each element contains 431 elements. i want to use mapply to sum the values from each list. For example, say I have a list of 5 elements, but each element has another 5 elements. > o [[1]] [1] 1 2 3 4 5 [[2]] [1]…
user1828605
  • 1,723
  • 1
  • 24
  • 63
4
votes
2 answers

Efficiently counting numbers falling within each range of numbers

I'm looking for a faster solution to the problem below. I'll illustrate the problem with a small example and then provide the code to simulate a large data as that's the point of this question. My actual problem size is of list length = 1 million…
Arun
  • 116,683
  • 26
  • 284
  • 387
3
votes
3 answers

How to apply a function on each row of a tibble and a nested list of data frames

I have the following tibble and a nested list of data frames: >source # A tibble: 6 × 2 lon lat 1 6.02 55.1 2 6.02 55.0 3 6.02 54.9 >dest [[1]][[1]] lon lat 1 54.98908 6.900084 2 54.92777 6.772623 3 …
Andreas
  • 397
  • 4
  • 18
  • 37
3
votes
2 answers

FALSE vs. F: Error mapply's SIMPLIFY argument: in SIMPLIFY=F

This Question refers to this Is there a best way to append a list values to a sublist of a list in R?. One of the solution is this: a <- list(3,5,7) l <- list(c(1,2,3), c(2,1,4), c(4,7,6)) mapply(c, l, a, SIMPLIFY=F) If I try to apply it on my…
TarJae
  • 72,363
  • 6
  • 19
  • 66
3
votes
1 answer

Extracting elements from a nested list to a data.matrix

I have a list of 2 elements that each has 3 elements. In every 3 elements, there is a list of length 2 nested elements so that each includes a tibble with rows and columns. tibbles' dimension is fixed for rows but the columns are different from the…
Maya_Cent
  • 471
  • 4
  • 10
3
votes
1 answer

R: using mapply for a function of two vectors

I have an R function that calculates the Hamming distance of two vectors: Hamming = function(x,y){ get_dist = sum(x != y, na.rm=TRUE) return(get_dist) } that I would like to apply to every row of two matrices M1, M2 without using a for loop. What I…
Max
  • 487
  • 5
  • 19
3
votes
2 answers

Ignoring NA in R across multiple columns of Datafrme using na.omit or NA.RM and mapply

I have a dataframe that looks somethings like this: SampleNo Lab1 Lab2 Lab3 lab4 lab5 lab6 lab7 lab8 lab9 lab10 1 59.84 60.59 60.39 60.29 60.19 60.32 60.24 60.3 60.43 NA 2 59.78 60.19 60.16 60.23 …
Spooked
  • 586
  • 1
  • 4
  • 16
3
votes
2 answers

How to loop through mapply in R?

I am trying to concatenate strings using mapply function in R. However, I want one of the strings to be variable in mapply function. I have a snippet of my code…
user86907
  • 817
  • 9
  • 21
3
votes
1 answer

Fetch daily data for each variable using mapply

I've a function which objective is to fetch daily data for each variable on a column on a data.frame. Range is a complete month, but could be any other range. My df has a column unit_id, so I need my function to take the first id of col unit_id and…
Omar Gonzales
  • 3,806
  • 10
  • 56
  • 120
1 2
3
36 37