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
6
votes
1 answer

r mapply vs "bad lapply"

I think I've missed something simple here: I have a list of data.frames, and a list of row numbers to select. Something like this: a <- data.frame(q = c(1,0,0,0), w = c(1,1,0,0), e = c(1,1,1,0), r =…
6
votes
1 answer

Efficient way to find manager's manager's id

I have a database of employees, with their manager's id, in long format (one row per employee per month). I would like to add a column that contains their manager's manager's id (or the id of their skip level manager). Here is a toy dataset: id <-…
BLT
  • 2,492
  • 1
  • 24
  • 33
6
votes
4 answers

remove multiple patterns from text vector r

I want to remove multiple patterns from multiple character vectors. Currently I am going: a.vector <- gsub("@\\w+", "", a.vector) a.vector <- gsub("http\\w+", "", a.vector) a.vector <- gsub("[[:punct:]], "", a.vector) etc etc. This is painful. I…
vagabond
  • 3,526
  • 5
  • 43
  • 76
6
votes
5 answers

Pasting elements of two vectors alphabetically

Say I have two vectors: a <- c("george", "harry", "harry", "chris", "steve", "steve", "steve", "harry") b <- c("harry", "steve", "chris", "harry", "harry", "george", "chris", "george") What I want to do is paste together the 1st pair, 2nd pair,…
jalapic
  • 13,792
  • 8
  • 57
  • 87
5
votes
3 answers

R use mapply on nested list

Using base R, I'd like to use the mapply function on a nested list. For example, in the code below, I'm trying to remove the letter "a" from each element of a nested list. I'd like to replace the last two lines with just a single line of…
user1491868
  • 596
  • 4
  • 15
  • 42
5
votes
2 answers

R: Using mapply with additional lists as arguments

I have the following list and vectors of parameters: myList <- list(c(3, 0, 1), c(2, 2, 2)) vPar1 <- c(1, 5, 100) vPar2 <- c(100, 5, 1) and I'm trying to draw samples from 3 Beta distributions with shape parameters shape1 = vPar1 and shape2 = vPar2…
Constantinos
  • 1,327
  • 7
  • 17
5
votes
2 answers

How to do faster list-column operations inside data.table

Due to memory (and speed) issues, I was hoping to do some computations inside a data.table instead of doing them outside it. The following code has 100.000 rows, but I'm working with 40 million rows. library(tictoc) library(data.table) # version…
Telaroz
  • 173
  • 1
  • 7
5
votes
2 answers

r how to use mapply with a data table

I want to do what seems a straightforward application of mapply in a data table. I want to multiply a series of data table columns by the value in another column. Here's my function. y is the single column to multiply the values in the other columns…
JerryN
  • 2,356
  • 1
  • 15
  • 49
5
votes
1 answer

Ways to improve for loop for matrix manipulations depending on another matrix

I know improving for loop has been asked tons of times before. We can apply family functions to improve the for loop in R. However is there a way to improve manipulations of a matrix where those manipulations depend on another matrix? What I mean…
rmania
  • 113
  • 4
5
votes
2 answers

Return multiple lists in mapply

I have a function which I am applying across a list of data frames or matrices (df) using mapply. The function outputs four different types of data frames (ex. a:d) based on some criteria of transformation of the original data frame, but I am…
Chelsea
  • 85
  • 4
4
votes
1 answer

Memory efficiency of rolling max / mins with variable windows in R

I've been working on an exercise where I'm needing to calculate max / mins with a variable window length on some large datasets (~100 - 250 million rows). In short, I have a table showing the start and end index (denoted by "Lookup_table" below)…
Phil
  • 261
  • 1
  • 8
4
votes
2 answers

Use ´mapply´ to create an output

I want to create an output with a bullet point for each entry Data I have one (and only one) row from my data frame: structure(list(Dimensions = 2L, Continuity = structure(2L, .Label = c("", "continuous"), class = "factor"), Differentiability =…
Jan
  • 4,974
  • 3
  • 26
  • 43
4
votes
2 answers

Pass function name as argument in mapply?

I would like to pass a function name as an argument in mapply: f2 <- function(a, b) a + b^2 f <- function(a, b, func) func(a, b) f(1, 3, f2) ## returns 10 mapply(f2, 1:2, 3) ## returns [1] 10 11 mapply(function(a, b) f(a, b, f2), 1:2, 3) ##…
Robert McDonald
  • 1,250
  • 1
  • 12
  • 20
4
votes
4 answers

mutate() a list based upon another list

In this example, I have two lists tiers <- list("tier 1", "tier 2", "tier 3") main <- list(data.frame(a = c("this", "that")), data.frame(a = c("the other", "that too")), data.frame(a = c("once more", "kilgore…
Steven
  • 3,238
  • 21
  • 50
4
votes
6 answers

Using mapply for indirect addressing in a data frame

With the following two data frames > d1 keystr keynum 1 abc 5 2 def 2 3 def 7 4 abc 3 > d2 HD 2 3 5 7 1 abc H I J K 2 def L M N P I would like to insert a column d1$val that uses the string in…
Vrokipal
  • 784
  • 5
  • 18
1
2
3
36 37