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

applying function across multiple list in R

I have a function that I would like called computeMASE to apply to 3 different lists forecast.list,train.list,test.list all of them have common values (ap,wi). I can use the function individually to the list as shown in the code below, but when I…
forecaster
  • 1,084
  • 1
  • 14
  • 35
3
votes
1 answer

Using mapply to calculate the mean of more than one list

The dataset firstList <- list(a = 1:3, b = 4:6) secondList <- list(c = 7:9, d = 10:12) I am trying to calculate the mean of multiple lists with mapply. mapply(mean, firstList, secondList) It did not work because mean only averages its first…
Ragy Isaac
  • 1,458
  • 1
  • 17
  • 22
3
votes
2 answers

Using lapply to list percentage of null variables in every column in R

I was given a large csv that is 115 columns across and 1000 rows. The columns have a variety of data, some is character-based, some is integer, etc. However, the data has a LOT of null variables of varying types (NA, -999, NULL, etc.). What I want…
3
votes
1 answer

Using Map with mget yields unexpected output

I have a number of lists :say list1, list2,....and list 100 and function myfun . With these, I can run Map(myfun,list1,list2,...,list100). However, I want to avoid writing all list numbers, and so I tried Map(myfun,mget(paste0("list",1:100))). But,…
user227710
  • 3,164
  • 18
  • 35
3
votes
1 answer

Using match.call() with mapply

I have a function that basically outputs a boolean condition as a string from the arguments (the details of the function don't matter here) makeClause <-function(Sex=c("NA", "male", "female"), SmokingHx=c("NA", "current", "former",…
AdrienF
  • 859
  • 1
  • 6
  • 19
3
votes
1 answer

Using an apply function on multiple subsets of a vector

I've been scrounging around for a solution for this for a while, but there doesn't seem to be anything that can solve this seemingly simple problem I'm having. Long story short, I'm looking to run a function on multiple subsets of a vector, but I am…
3
votes
1 answer

Is there an efficient alternative to mapply in R?

Here is a sample data and code I am trying to test: mydata<-structure(list(mpg = c(21, 21, 22.8, 21.4), cyl = c(6, 6, 4, 6), disp = c(160, 160, 108, 258), hp = c(110, 110, 93, 110)), .Names = c("mpg", "cyl", "disp", "hp"), class = "data.frame",…
Metrics
  • 15,172
  • 7
  • 54
  • 83
2
votes
2 answers

Use mapply() within attr()

I am running the function below, but adist() is not vectorized, so I need to run it using rowwise(). Obviously this is very slow with a large amount of data. In my scenario, I only have current_text and previous_text, and change is generated from…
Adam_G
  • 7,337
  • 20
  • 86
  • 148
2
votes
3 answers

Weird behavior of mapply with rep and dplyr pipes in R

I am dealing with strings having two separators "*" and "|", and they are used in strings such as: "3\*4|2\*7.4|8\*3.2" Where the number right before "*" denotes frequency and the float or integer right after "*" denotes value. These value…
2
votes
1 answer

Efficient and fast application of a function to 3D arrays in R

I have a very large 3D array (say 100 x 100 x 10) that I would like to apply a function over for pairwise comparisons. I've tried a number of solutions, using data.table, mapply, etc. I'm maybe naively hoping for faster speedups, and am considering…
Eric Ward
  • 23
  • 3
2
votes
2 answers

2 consecutive nested for-loops - Is it possible to optimize with data.table or apply? Added data.table attempt yet to be optimized

I have a data.table made of data.tables as per the dput at the end of this question. I manipulate this data.table of data.tables using the following nested for-loops: test_E2 <- list() for (i in unique(lst_512_32_E2$ID)){ test_E2[[i]] <-…
Akthem
  • 53
  • 7
2
votes
1 answer

R apply with a vector parameter

I am trying to apply the ppoibin (poisson binomial probabilities) function from the poibin package to each row in a dataframe. This function takes two parameters: an integer and a vector of probabilities. My data takes the form: p <- matrix(c(0.046,…
Zachary
  • 345
  • 1
  • 8
2
votes
2 answers

How to apply a function to every element in a dataframe in a list and return a dataframe in R?

I have a dataframe which looks like this example (just much larger): Name <- c('Peter','Peter','Peter', 'Ben','Ben','Ben','Mary', 'Mary', 'Mary') var1 <- c(0.4, 0.6, 0.7, 0.3, 0.9, 0.2, 0.4, 0.6, 0.7) var2 <- c(0.5, 0.4, 0.2, 0.5, 0.4, 0.2, 0.1,…
Mina
  • 117
  • 7
2
votes
1 answer

Applying custom function to a list of DFs, taking another list as an input - R

I have a list of dfs and a list of annual budgets. Each df represents one business year, and each budget represents a total spend for that year. # the business year starts from Feb and ends in Jan. # the budget column is first populated with the %…
2
votes
1 answer

How to replace rownames of a list of dataframes with values from a list of character strings in R?

Let l be a list of two dataframes and n a list of character strings, wherein l and n both have two elements. Also, let the number of rows of each dataframe in l be equal to the length of the vector of character strings in n. l <- list(data.frame(x =…
Dion Groothof
  • 1,406
  • 5
  • 15