mapply

Combine list elements according to common dataframe value

 ̄綄美尐妖づ 提交于 2019-12-13 00:28:45
问题 A followup to this question here, even though the example is specific, this seems like a generalizable application, so I think it's worth a separate thread: The general question is: How do I take elements in a list that correspond to a value in an original data frame and combine them according to that value in the original data frame, especially when the elements of the list are of different length? In this example, I have a dataframe that has two groups, each sorted by date. What I

Using tryCatch in the integral and mapply

橙三吉。 提交于 2019-12-12 03:53:44
问题 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, 0.944579005), i1 = c(0.997434318, 0.997434318, 0.997434318, 0.997434318), x1b1m = c(2.938615084, 3.220915079, 4.088527679, 1.818297207), x1b1e = c(3.561241746, 3.843541741, 4.711154342, 1.594420075), x1b0m = c(2.714737952, 2.997037947, 3.864650548, 1

Command inside mapply command in mvtnorm package

柔情痞子 提交于 2019-12-11 11:44:55
问题 I would like a vector with the different values of cumulative probability of a bivariate when some parameters change value simultaneously according to different function. I've dome with "mapply" command as in this case: library(mvtnorm) m1<-c(1,2,3,4,5,5,5,5,5,5) m2<-c(-1,-2,-3,-4,-5,-5,-5,-5,-5,-5) m3<-c(0,0,0,0,0,1,2,3,4,5) mapply(function(x,y,z) pmvnorm(mean = c(18,12.72,(18*(x+y)+12.72*z)),sigma=matrix(c(5.7,0,5.7*(x+y),0,30.38,30.38*z,5.7*(x+y),30.38*z,5.7*(x+y)^2+30.38*(z)^2),3), lower

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

好久不见. 提交于 2019-12-11 06:14:18
问题 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 <- function(relGPA, avgGPA) 1/(1+exp(sum(relGPA*pred.model$coef[1],avgGPA*pred.model$coef[2]))) and all I want is a data.frame with 3 columns for the avgGPA values, and 16 rows for the relGPA values with the resulting values in the cells. I apologize for how

How to pass arguments to function called from mapply directly OR how to treat vector as argument to function not to mapply

两盒软妹~` 提交于 2019-12-11 04:42:51
问题 Suppose I have following function: my.fun1 <- function(a,b,c){ a * c + b } If I want to call it several times with multiple arguments I can do: > my.fun1(1, 1, c(1:3)) [1] 2 3 4 > my.fun1(2, 2, c(1:3)) [1] 4 6 8 > my.fun1(3, 3, c(1:3)) [1] 6 9 12 But if I use mapply I get this: > mapply(my.fun1, c(1:3), c(1:3), c(1:3)) [1] 2 6 12 Instead of desired: [[1]] [1] 2 3 4 [[2]] [1] 4 6 8 [[3]] [1] 6 9 12 IMHO the problem is that mapply basically translates the function call to: > my.fun1(1, 1, 1) [1

Using mapply with mean function on a matrix

ε祈祈猫儿з 提交于 2019-12-10 15:24:53
问题 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 loop), to calculate the mean of the first 2 values in each column, and plan to apply this to the whole matrix row-by-row. However mapply which seems to work if I try to sum the values but not for the mean function. See example below: x <- matrix(c

remove multiple patterns from text vector r

若如初见. 提交于 2019-12-08 07:04:33
问题 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 was looking at this question & answer: R: gsub, pattern = vector and replacement = vector but it's not solving the problem. Neither the mapply nor the mgsub are working. I made these vectors remove <- c("@\\w+", "http\\w+", "[[:punct:]]") substitute <

mapply for all arguments' combinations [R]

狂风中的少年 提交于 2019-12-07 05:40:36
问题 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 different combinations of those arguments. So I thought, ok, let's use the multivariate version of the apply functions mapply . mapply(FUN = toy, x = 1:2, y = c("#", "$"), z = c("a", "b")) [1] "1 # a" "2 $ b" But this is not quite what I wanted. Indeed,

Efficiently counting numbers falling within each range of numbers

吃可爱长大的小学妹 提交于 2019-12-04 11:08:12
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 entries. Say, I've two lists as shown below: x <- list(c(82, 18), c(35, 50, 15)) y <- list(c(1,2,3,55,90), c(37,38,95)) Properties of x and y: Each element of the list x always sums up to 100. Each element of y will always be sorted and will be always between 1 and 100. The problem: Now, what I'd like is this. Taking x[[1]] and y[[1]] , I'd like to

Apply a function over all combinations of arguments

♀尐吖头ヾ 提交于 2019-12-04 08:03:48
问题 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 anything. Is there a better solution? # Apply function FUN to all combinations of arguments and append results to # data frame of arguments cmapply <- function(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE) { l <- expand.grid(...,