logo
Tags down

shadow

R speed up sapply


By : guillermo.elia
Date : October 17 2020, 08:10 AM
hope this fix your issue I've got the following script in a loop: , Gregor, This is probably better:
code :
 x <- similar_addresses[, .N, by = Original_Address]$N
 y <- similar_addresses[, length(unique(people_names)) , by = Original_Address]$V1
 number_of_rows_similar_addresses <- cbind(unique(similar_addresses$Original_Address), x/y)


Share : facebook icon twitter icon

Speed comparison of sapply with a composite function


By : user2597470
Date : March 29 2020, 07:55 AM
I wish this helpful for you There's nothing here to sapply to - you only give it a single vector - not a list of vectors, and sapply converts the result to a (single column) matrix.
sapply is simplifying the result for you, but in doing so has to generate an array.
code :
system.time(sapply(list(rnorm(1000000,0,1)), function (x) round(x,2))) 
user  system elapsed 
 0.22    0.00    0.22 

system.time(sapply(rnorm(1000000,0,1), function (x) round(x,2))) 
user  system elapsed 
4.21    0.00    4.21 

Speed up double nested sapply + lmer function in R


By : GeorgeV
Date : March 29 2020, 07:55 AM
hop of those help? sapply and so forth won't save you much time, they're just cleaner (sometimes). On the other hand, once you have scrambled your factor columns you can use refit to re-fit the model for different response data (i.e. columns) at some savings in time.
I used the plyr package for a couple of things below.
code :
library(lme4)
library(plyr)
set.seed(101)
##Generate real data
real.data=data.frame(cat1=factor(rep(c("A","B","C","D"),500)),
cat2 <- factor(rep(c("E","F","G","H"),500)), matrix(runif(12000),ncol=6))
sfun <- function() {
###Generate a randomized data set by sampling first two factor columns
    sampledat <- transform(real.data,
                           cat1=factor(sample(cat1)),
                           cat2=factor(sample(cat2)))
    ## fit first column
    m1 <- lmer(X1 ~ (1|cat1)+(1|cat2)+(1|cat1:cat2), data=sampledat)
    ## refit using every other column
    m_rest <- apply(real.data[,-(1:3)],2,
                     refit,object=m1)
    ## note this is 'laply' (from plyr), not 'lapply'
    laply(c(list(m1),m_rest),function(m) unlist(VarCorr(m)))
}
nsim <- 50
sres <- raply(nsim,sfun(),.progress="text")

How can I speed up this sapply for cross checking samples?


By : Loan Tricot
Date : March 29 2020, 07:55 AM
this one helps. Here is a vectorized version of your code, about 20 times faster on my machine:
code :
rowMeans(microarrayScores[mycombs[,1], ] ==
         microarrayScoresQC[mycombs[,2], ], na.rm = TRUE)

how to avoid a loop or sapply to speed up a computation


By : Ananthu Chakravarthi
Date : March 29 2020, 07:55 AM
it fixes the issue We group_by(t, id, h, d) then calculate the Sum for each row then finally calculate the total sum result for rows that have the same t, id, h, d
code :
library(tidyverse)

set.seed(1)
options(scipen = 999)

dat <- data.frame(id = rep(c(rep(1,5), rep(2,5)),2), h = rep(1:2,5), 
                   d = c(rep(1,10), rep(2,10)), 
                   t = rep(c(sample(c(1,2,3), 5, replace = T),
                   sample(c(1,2,3), 5, replace = T)),2), q = runif(20), p = runif(20),
                   b = runif(20), w = rep(c(rep(.1,2), rep(.2,2)),5))
dat

#>    id h d t          q         p          b   w
#> 1   1 1 1 1 0.20597457 0.4820801 0.47761962 0.1
#> 2   1 2 1 2 0.17655675 0.5995658 0.86120948 0.1
#> 3   1 1 1 2 0.68702285 0.4935413 0.43809711 0.2
#> 4   1 2 1 3 0.38410372 0.1862176 0.24479728 0.2
#> 5   1 1 1 1 0.76984142 0.8273733 0.07067905 0.1
#> 6   2 2 1 3 0.49769924 0.6684667 0.09946616 0.1
#> 7   2 1 1 3 0.71761851 0.7942399 0.31627171 0.2
#> 8   2 2 1 2 0.99190609 0.1079436 0.51863426 0.2
#> 9   2 1 1 2 0.38003518 0.7237109 0.66200508 0.1
#> 10  2 2 1 1 0.77744522 0.4112744 0.40683019 0.1
#> 11  1 1 2 1 0.93470523 0.8209463 0.91287592 0.2
#> 12  1 2 2 2 0.21214252 0.6470602 0.29360337 0.2
#> 13  1 1 2 2 0.65167377 0.7829328 0.45906573 0.1
#> 14  1 2 2 3 0.12555510 0.5530363 0.33239467 0.1
#> 15  1 1 2 1 0.26722067 0.5297196 0.65087047 0.2
#> 16  2 2 2 3 0.38611409 0.7893562 0.25801678 0.2
#> 17  2 1 2 3 0.01339033 0.0233312 0.47854525 0.1
#> 18  2 2 2 2 0.38238796 0.4772301 0.76631067 0.1
#> 19  2 1 2 2 0.86969085 0.7323137 0.08424691 0.2
#> 20  2 2 2 1 0.34034900 0.6927316 0.87532133 0.2

dat %>% 
  group_by(t, id, h, d) %>% 
  mutate(Sum = q * pnorm((p - b)/w)) %>% 
  mutate(result = sum(Sum))

#> # A tibble: 20 x 10
#> # Groups:   t, id, h, d [18]
#>       id     h     d     t      q      p      b     w          Sum  result
#>    <dbl> <int> <dbl> <dbl>  <dbl>  <dbl>  <dbl> <dbl>        <dbl>   <dbl>
#>  1    1.     1    1.    1. 0.206  0.482  0.478  0.100 0.107        8.76e-1
#>  2    1.     2    1.    2. 0.177  0.600  0.861  0.100 0.000784     7.84e-4
#>  3    1.     1    1.    2. 0.687  0.494  0.438  0.200 0.419        4.19e-1
#>  4    1.     2    1.    3. 0.384  0.186  0.245  0.200 0.148        1.48e-1
#>  5    1.     1    1.    1. 0.770  0.827  0.0707 0.100 0.770        8.76e-1
#>  6    2.     2    1.    3. 0.498  0.668  0.0995 0.100 0.498        4.98e-1
#>  7    2.     1    1.    3. 0.718  0.794  0.316  0.200 0.712        7.12e-1
#>  8    2.     2    1.    2. 0.992  0.108  0.519  0.200 0.0199       1.99e-2
#>  9    2.     1    1.    2. 0.380  0.724  0.662  0.100 0.278        2.78e-1
#> 10    2.     2    1.    1. 0.777  0.411  0.407  0.100 0.403        4.03e-1
#> 11    1.     1    2.    1. 0.935  0.821  0.913  0.200 0.302        3.75e-1
#> 12    1.     2    2.    2. 0.212  0.647  0.294  0.200 0.204        2.04e-1
#> 13    1.     1    2.    2. 0.652  0.783  0.459  0.100 0.651        6.51e-1
#> 14    1.     2    2.    3. 0.126  0.553  0.332  0.100 0.124        1.24e-1
#> 15    1.     1    2.    1. 0.267  0.530  0.651  0.200 0.0728       3.75e-1
#> 16    2.     2    2.    3. 0.386  0.789  0.258  0.200 0.385        3.85e-1
#> 17    2.     1    2.    3. 0.0134 0.0233 0.479  0.100 0.0000000356 3.56e-8
#> 18    2.     2    2.    2. 0.382  0.477  0.766  0.100 0.000735     7.35e-4
#> 19    2.     1    2.    2. 0.870  0.732  0.0842 0.200 0.869        8.69e-1
#> 20    2.     2    2.    1. 0.340  0.693  0.875  0.200 0.0615       6.15e-2

Speed improvement for sapply along a POSIX sequence


By : moe.junro
Date : March 29 2020, 07:55 AM
hop of those help? You could try using data.tables new foverlaps function. With the data from the other question:
Related Posts Related Posts :
  • Get the min value of one variable greater than the observation of another variable
  • Reference previous value in data.table calculation
  • 'Can't create call to non-callable object' error while summarizing data by grouping
  • Multiplying all columns in dataframe by single column
  • Calculating Standard Deviation by each unique variable in R.
  • Fisher exact test for each gene
  • Sorting coordinates to create a polygon gives messy results
  • Merging legends in ggplot fails with certain expressions
  • Shiny modal dialog in response to multiple buttons
  • Error connecting to mongoDB using Mongolite
  • R: networkD3 sankey plot - colours not displaying
  • Merging two rows into one header using R
  • Parse JSON to dataframe returns different numbers
  • How to number by group?
  • Multinomial probit regression with mixed type explanatory variables
  • How can I make a variable in a dataset containing a vector of all numbers between two other variables?
  • How to extract the trailing digits from a string in R?
  • Select values based on other columns
  • readLines killing R in purrr::map
  • Subset rows based on "start and stop" strings
  • How to add a column to lists within a list without losing their names?
  • Plotting the means in ggplot, without using stat_summary()
  • R :Looping through each 5 rows of data frame and imputing incremental value
  • In R, is growing a list just as inefficient as growing a vector?
  • Flexdashboard, rhandsontable: how to programmatically access user updated table?
  • Creating Summary Table from R Variables
  • Average over groups and include previous groups
  • R: data.table count rows on specific columns > 0
  • Transform (shuffle) just 2 Fields in a Dataframe
  • Issue with replacing string by match in R
  • (very) Simple quantstrat trading model using logistic regression
  • R - count maximum number of consecutive dates
  • Problems using tidyr separate on "|"
  • Default value when calling a function in a for loop
  • Finding values in a matrix from list of values in R
  • count 0's in a zoo (or dataframe) object
  • Finding the first non-zero year in data frame for multiple variables using tidyverse
  • ggplot2 - how to assign geom_text with arrow icon to second yaxis scale
  • regex fails with dollar sign
  • Drop first element of list of lists, condense list of lists? Too many elements?
  • R - how to apply output of ifelse(str_detect ...) to whole group
  • caret package confusion matrix define positive case with multiple classes
  • Generating a pairwise 'distance' matrix
  • Change all R columns names using a reference file
  • In R & dabestr, how do I get grouped differences correctly?
  • Exclude or set a unique color to the bottom triangle of a correlation matrix heatmap
  • r shiny observe function clears text input
  • Split column by multiple delimiters, keeping delimiters
  • How to random search in a specified grid in caret package?
  • merge 2 data frames in a loop for each column in one of them
  • how to edit the codes for the summary of R S4 Object?
  • Remove specific rows in R
  • Flatten JSON list into data frame
  • Filtering a dataset and making a ggplot
  • Align cells vertically to be at the bottom flextable
  • invalid subscript type 'list' Azure Machine Learning
  • Use rollapply with xts object and an anonymous defined function
  • Isolate data frames from a spreadsheet to create a list
  • Error in xts, as.POSIXct "'order.by' cannot contain 'NA', 'NaN', or 'Inf'"
  • Column splitting in R
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org