mapply

Mapply error after updating R and tidyverse

蹲街弑〆低调 提交于 2020-06-17 09:35:10
问题 I have been working on a rejection sampling code using several loops. After updating R and tidyverse I found that the code no longer works, displaying the following error: Error: Assigned data `mapply(...)` must be compatible with existing data. i Error occurred for column `sampled`. x Can't convert from <integer> to <logical> due to loss of precision. * Locations: 1. Run `rlang::last_error()` to see where the error occurred. In addition: Warning message: In seq.default(x, y, na.rm = TRUE) :

mcmapply performance on multiple cores

落花浮王杯 提交于 2020-01-23 17:52:14
问题 I have a function which I want to run on around 3 million datapoints. I am trying to parallelise the function using mcmapply on a Ubuntu machine with 8 cores. The function takes in a list of length 3 million as well as 3 more vectors of length 3 million and 1 constant value cutoffyearmon . The code runs perfectly fine with 100000 rows of data within 2 minutes on a single core and throws no error. However, when I try to run the code in parallel on 6 cores of my machine using mcmapply it keeps

Vectorize() vs apply()

限于喜欢 提交于 2020-01-20 03:16:07
问题 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 is also useful to Vectorize() when I am going to be using that vectorized function multiple times in my R code. For instance: a <- 100 b <- 200 c <- 300 varnames <- c('a', 'b', 'c') getv <- Vectorize(get) getv(varnames) vs sapply(varnames, get)

Efficiently counting numbers falling within each range of numbers

最后都变了- 提交于 2020-01-13 03:44:47
问题 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

Non-standard evaluation of subset argument with mapply in R

吃可爱长大的小学妹 提交于 2020-01-05 09:47:29
问题 I can not use the subset argument of any function with mapply . The following calls fail with the subset argument, but they work without: mapply(ftable, formula = list(wool ~ breaks, wool + tension ~ breaks), subset = list(breaks < 15, breaks < 20), MoreArgs = list(data = warpbreaks)) # Error in mapply(ftable, formula = list(wool ~ breaks, wool + tension ~ : # object 'breaks' not found mapply(xtabs, formula = list(~ wool, ~ wool + tension), subset = list(breaks < 15, breaks < 20), MoreArgs =

Non-standard evaluation of subset argument with mapply in R

 ̄綄美尐妖づ 提交于 2020-01-05 09:47:14
问题 I can not use the subset argument of any function with mapply . The following calls fail with the subset argument, but they work without: mapply(ftable, formula = list(wool ~ breaks, wool + tension ~ breaks), subset = list(breaks < 15, breaks < 20), MoreArgs = list(data = warpbreaks)) # Error in mapply(ftable, formula = list(wool ~ breaks, wool + tension ~ : # object 'breaks' not found mapply(xtabs, formula = list(~ wool, ~ wool + tension), subset = list(breaks < 15, breaks < 20), MoreArgs =

R: How to run a function/calculation on two lists based on their names?

醉酒当歌 提交于 2019-12-31 05:21:05
问题 I want to run a function (in this case just a multiplication) on two list based on their names. Here some example data showing my structure: A <- list("111"=matrix(sample(1:10,9), nrow=3, ncol=3), "112"=matrix(sample(1:10,9), nrow=3, ncol=3)) names <- list(c("A", "B", "C"), c("A", "B", "C")) A <- lapply(ProdValues, function (x) {dimnames(x) <- names; return(x)}) List A has values in matrices for different products (listnames=111,112) and List B (below) has YEARLY values for the same products,

Applying a function to two lists?

妖精的绣舞 提交于 2019-12-28 03:01:12
问题 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 <- matrix(rnorm(2000), nrow=10) sapply(1:10, function(row) cor(X[row,], Y[row,])) Now, how should I apply this function to two lists (containing around 50 dataframes each)? Consider list A has dataframes $1, $2, $3... and so on and list B has similar number of

How to calculate means across rows of three multi-column dataframes?

不打扰是莪最后的温柔 提交于 2019-12-25 04:30:52
问题 Let's say I have 3 data frames, each a 5x5 object as such: set.seed(1) x <-as.data.frame(matrix(rnorm(10),ncol=5,nrow=5)) colnames(x) <-c("a","b","c","d","e") y <-as.data.frame(matrix(rnorm(10),ncol=5,nrow=5)) colnames(y) <-c("f","g","h","i","j") z <-as.data.frame(matrix(rnorm(10),ncol=5,nrow=5)) colnames(z) <-c("k","l","m","n","o") So, x, for instance, looks like: > x a b c d e 1 -0.6264538 -0.8204684 -0.6264538 -0.8204684 -0.6264538 2 0.1836433 0.4874291 0.1836433 0.4874291 0.1836433 3 -0

How to use mapply to calculate CCF for list of pairs of time series?

☆樱花仙子☆ 提交于 2019-12-25 02:40:05
问题 I am trying to apply functions described here for a set of time series. For this, mapply seems to be a good approach but I guess there is some problem either in defining the function or in using mapply. Here is the example code, where I found some discrepancy in the format of dataframe being returned and might be the source of error. # define the function to apply ccffunction <- function(x, y, plot = FALSE){ ts1 = get(x) ts2 = get(y) d <- ccf(ts1, ts2,lag.max = 24, plot = plot) cor = d$acf[,