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

mapply with arguments as two lists (one formulas, one vectors)

I'm trying to use mapply to apply t.test over two lists of arguments. The first list formulas contains three formulas and the second list periods contains three vectors that subset my.data, which I pass with the MoreArgs argument. I can do the…
Richard Herron
  • 9,760
  • 12
  • 69
  • 116
0
votes
1 answer

lapply to multiple arguments in a function

I am using the lapply function in the following form: y=rnorm(10) x1=c(1,1,1,2,2,2,3,3,3,4) x2=c(5,5,6,6,7,7,8,8,9,9) x3=c(7,7,8,8,9,9,10,10,11,11) t1 <- outer(x1, unique(x1), '<=') t2 <- outer(x2, unique(x2), '<=') t3 <- outer(x3, unique(x3),…
user17645
  • 65
  • 7
0
votes
3 answers

How to calculate the weighted value without loop?

Sorry for asking trivial question. Here is my example data: (x <- data.frame(period=c('20130101','20130102'),symbol=c('x1','x2'),V1=c(1,2),V2=c(3,4))) (y <- data.frame(period=c('20130101','20130101','20130102','20130102'), …
YYY
  • 605
  • 3
  • 8
  • 16
0
votes
1 answer

Using tryCatch in the integral and mapply

I have the date (test): test<-structure(list(X = c(630L, 631L, 977L, 978L), si = c(1.063166618, 1.063166618, 1.063166618, 1.063166618), ho = c(-0.071587466, -0.071587466, -0.071587466, -0.071587466), bd = c(0.998351574, 0.999361098, 0.999978304,…
Metrics
  • 15,172
  • 7
  • 54
  • 83
0
votes
1 answer

R: comparing two character columns in a big data frame

So, I have s really huge data frame, which has two columns of characters. the characters are some ID values separated by ";". So, I want to calculate the number of common ID values between these two columns. Here is an example: id.x …
Vahid Mirjalili
  • 6,211
  • 15
  • 57
  • 80
-1
votes
1 answer

Conditional aggregates for every row of a data frame and append it as a new column value

I have tried to write some code to do conditional aggregates for every row of a data frame and append it as a new column value. The example is calculating season average statistics for every team in the nab game at a point in the NBA season. This…
TeeseCaprice
  • 55
  • 1
  • 13
-1
votes
2 answers

mapply on pairs of elements in a lists in R

If I have a symmetric binary operator that I want to apply over the pairs of elements from a list, is there an easy way I can do this in R? I tried: A <- list(1,2,3) mapply(function(x,y) x+y, A,A) but this only gives x[n]+y[n] for all n=1..N but I…
monotonic
  • 394
  • 4
  • 20
-1
votes
1 answer

How applying a function to multiple lists and get a list as the output in R?

I have two lists A and B that each of which has 100 elements. Each element is a square matrix, N in N. I want to multiply element i in list A with element i in list B (I between 1 and 100) and having a list of 100 elements as the output. How I can…
Amin Kaveh
  • 117
  • 6
-1
votes
1 answer

Using lists and mapply on user created function for sum coding contrasts in r

I want to use lists and mapply on user created function for sum coding contrasts in r. But when I try it does not work. Any help would be appreciated. Specifically, I want to apply sum contrasts to am and vs to create the sum-coding variables am_c…
Mel
  • 510
  • 3
  • 10
-1
votes
1 answer

ggplot and lapply /mapply for nested list and data frames

Edit: I did find a way to do what I need, but now I'm having trouble getting a title to appear for each of the plots that are created so I know which site I am looking at: lapply(seq(gl), function(i){ lapply(seq(gl[[i]]), function(j){ …
-1
votes
1 answer

Truncation and merging values in two character vectors

I have a character vector V1 V1 <- c("377 Peninsula St. Ogden,UT","8532 West Lyme St. Chesterfield, VA","43 E. Hilltop Street Hilliard,OH","95 Newcastle St. Hendersonville,NC","7276 Rose St. Greenville,NC") and another vector as V2 V2 <-…
Deep
  • 528
  • 3
  • 12
  • 27
-1
votes
1 answer

Faster way than mapply function in R

I recently asked a question for which I got a useful answer but the following code runs very slowly considering my big dataset (14K rows). How can I make this faster using mcmapply like here? this is the code: within(df, count <-…
Amleto
  • 584
  • 1
  • 7
  • 25
-1
votes
3 answers

Apply Function to create dataset from the dataframes column

I have a dataset as shown below The challenge is to apply function on the columns from the 3rd column till the end. The Function should Filter the dataset, column having only false and create a dataframe by the name as column name as shown below…
Shivpe_R
  • 1,022
  • 2
  • 20
  • 31
-1
votes
1 answer

Using mapply over a matrix when the function makes use of elemets' location in the matrix

I want to use mapply to apply fun to all elements of a matrix. I'm not sure how to use mapply when the function to be applied makes use of each elements' location in the matrix. fun <- function(theta, mat, i, j){ sum_nearby <-…
Ryan
  • 934
  • 8
  • 14
-1
votes
1 answer

Using lapply for multiple lists in a function

I have 2 lists of matrices A and B: n<-10 generate<-function (n){ matrix(runif(10*10),ncol = 10) } A<-lapply(1:n, generate) B<-lapply(1:n, generate) I am trying to use lapply to run it through a function with two inputs. Where my function is a…
adaodante
  • 63
  • 2
  • 7
1 2 3
36
37