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

R data.table apply function to rows using columns as arguments

I have the following data.table x = structure(list(f1 = 1:3, f2 = 3:5), .Names = c("f1", "f2"), row.names = c(NA, -3L), class = c("data.table", "data.frame")) I would like to apply a function to each row of the data.table. The function func.test…
broccoli
  • 4,738
  • 10
  • 42
  • 54
31
votes
1 answer

Applying a function to two lists?

To find the row-wise correlation of two matrices X and Y, the output should have a correlation value for row 1 of X and row 1 of Y, ..., hence in total ten values (because there are ten rows): X <- matrix(rnorm(2000), nrow=10) Y <-…
Paul
  • 1,077
  • 3
  • 14
  • 27
21
votes
2 answers

Force mapply to return a list?

Suppose I have a function that creates data frames. I'd like to run that function with different input values, and then rbind the results together into one big data frame, as below: CreateDataFrame <- function(type="A", n=10, n.true=8) { …
Adrian
  • 3,138
  • 2
  • 28
  • 39
18
votes
2 answers

Apply a function over all combinations of arguments

I would like to be able to apply a function to all combinations of a set of input arguments. I have a working solution (below) but would be surprised if there's not a better / more generic way to do this using, e.g. plyr, but so far have not found…
waferthin
  • 1,582
  • 1
  • 16
  • 27
15
votes
5 answers

Avoiding multiple for-loops in R to calculate a matrix

So in the course of generating some fake data to answer a map question, I found myself writing the following: # Generate some fake data lat <- seq(-90, 90, by = 5) lon <- seq(-180, 180, by = 10) phi <- matrix(0, nrow = length(lat), ncol =…
Mike Wise
  • 22,131
  • 8
  • 81
  • 104
14
votes
3 answers

R: apply a function to every element of two variables respectively

I have a function with two variables x and y: fun1 <- function(x,y) { z <- x+y return(z) } The function work fine by itself: fun1(15,20) But when I try to use it with two vectors for x and y with an apply function I do not get the correct…
adam.888
  • 7,686
  • 17
  • 70
  • 105
14
votes
2 answers

Vectorize() vs apply()

The Vectorize() and the apply() functions in R can often be used to accomplish the same goal. I usually prefer vectorizing a function for readability reasons, because the main calling function is related to the task at hand while sapply is not. It…
andrew
  • 2,524
  • 2
  • 24
  • 36
10
votes
3 answers

R data.table column names not working within a function

I am trying to use a data.table within a function, and I am trying to understand why my code is failing. I have a data.table as follows: DT <- data.table(my_name=c("A","B","C","D","E","F"),my_id=c(2,2,3,3,4,4)) > DT my_name my_id 1: A …
Sam
  • 103
  • 6
9
votes
3 answers

Non-standard evaluation of subset argument with mapply in R

I can not use the subset argument of xtabs or aggregate (or any function I tested, including ftable and lm) with mapply. The following calls fail with the subset argument, but they work without: mapply(FUN = xtabs, formula = list(~ wool, …
Thomas
  • 457
  • 2
  • 12
8
votes
1 answer

quantmod::chart_Series and mapply gives error with chart parameters

How do I use MoreArgs properly with chart_Series? p.txt s,n ABBV,AbbVie BMY,Bristol LLY,EliLily MRK,Merck PFE,Pfizer sof.r # R --silent --vanilla <…
AG1
  • 6,648
  • 8
  • 40
  • 57
8
votes
1 answer

mapply for all arguments' combinations [R]

Consider this toy function that receives 3 arguments: toy <- function(x, y, z){ paste(x, y, z) } For each argument I have a number of values (not necessarily the same number for each argument) and I want to apply the toy function to the…
elikesprogramming
  • 2,506
  • 2
  • 19
  • 37
8
votes
1 answer

Why does data.table get copied when returned from Map

I understood that data.table is not copied when returned from a function. However, in this particular case it does get copied. Can one explain why? dt1 <- data.table(a=1) dt2 <- data.table(b=1) dt3 <- data.table(c=1) address(dt1); address(dt2);…
imsc
  • 7,492
  • 7
  • 47
  • 69
8
votes
2 answers

Row-wise sort then concatenate across specific columns of data frame

(Related question that does not include sorting. It's easy to just use paste when you don't need to sort.) I have a less-than-ideally-structured table with character columns that are generic "item1","item2" etc. I would like to create a new…
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
7
votes
1 answer

mapply basics? - how to create a matrix from two vectors and a function

I am trying create a data.frame from which to create a graph. I have a function and two vectors that I want to use as the two inputs. This is a bit simplified, but basically all I have is: relGPA <- seq(-1.5,1.5,.2) avgGPA <- c(-2,0,2) f <-…
Sam Swift
  • 913
  • 1
  • 11
  • 17
6
votes
2 answers

Apply different functions to different columns programmatically in data.table R

I need to programmatically apply different functions to different columns and group by, using data.table. If the columns and functions were known, I would do like this: library(data.table) DT = data.table(id = rep(letters[1:3], each=3), …
Duccio A
  • 1,402
  • 13
  • 27
1
2 3
36 37