Generate random times in sample of POSIXct

I want to generate a load of POSIXct dates. I want to have the time component only between 9am and 5pm and only at 15 minute blocks. I know how to generate the random POSIXct between certain dates but how do I specify the minute blocks and the time r...
more »

2017-08-11 12:08 (3) Answers

shorthand names for long expressions in R

When constructing a R code, for example, for plotting some stuff, often it is necessary to add subsequent "layers" (labels, axes, markers) etc coded with complicated expressions. The example here comes from the "leaflet" package, and layers are added...
more »

2017-08-10 22:08 (1) Answers

Command Lines error in Rstudio console

how to assign a very long string to a variable in r? Following works fine testVar <- "test;test;" But the following gives a weird + in the RStudio console. testVar <- "test;test;test;test;test;test;test;test;test;test;test;test;test;t...
more »

2017-08-10 16:08 (1) Answers

PCA analysis wrong output

That's the example data: structure(c(368113, 87747.35, 508620.5, 370570.5, 87286.5, 612728, 55029, 358521, 2802880, 2045399.5, 177099, 317974.5, 320687.95, 6971292.55, 78949, 245415.95, 50148.5, 67992.5, 97634, 56139.5, 371719.2, 80182.7, 612078....
more »

2017-08-10 11:08 (1) Answers

subset multiple times a data frame

I want to subset 100 times a data frame that consists of 20 variables (continuous and categorical) in two parts that represent 70% and 30%. But I can do this with iris data set too as an example. data(iris) test.rows <- sample(1:nrow(iris), 105...
more »

2017-08-09 11:08 (2) Answers

r - Conditionally subtract two data tables

I have two data.tables showing temperatures for multiple locations (identified by geocode). I would like to create a third one based on a subtraction of them. Here they are: library(data.table) # Generate random data: geocode <- paste0("N", 1:...
more »

2017-08-08 20:08 (2) Answers

function environment within lapply loop

so i came across this problem that is to do with variables existing in different environments and it left me very confused as it does not fit with my understanding of how functions look for various objects. my toy example is very simple: i have a f...
more »

2017-08-08 11:08 (2) Answers

R: 'Multiply' dataframe columns of strings

Not sure what this operation is called, and Google did not help. Say I have two simple dataframes like this: df1 <- data.frame(factor1 = c("a", "b", "c")) df2 <- data.frame(factor2 = c("x", "y", "z")) > df1 factor1 1 a 2 b...
more »

2017-08-08 02:08 (2) Answers

Balls in Bins simulation for R

I would like to simulate the distribution for a fixed number of balls m in a fixed number of bins n in R. Up till now I have been using the Poisson approximation with rpois(). This is a decent approximation for a large number of balls in n bins. How...
more »

2017-08-07 17:08 (1) Answers

Group vector on conditional sum

I want to group a vector based on the sum of the elements being less than or equal to n. Assume the following, set.seed(1) x <- sample(10, 20, replace = TRUE) #[1] 3 4 6 10 3 9 10 7 7 1 3 2 7 4 8 5 8 10 4 8 #Where, n = 15 The ...
more »

2017-08-07 16:08 (2) Answers

Unused argument - corrplot?

I am a novice at this. I wanted to create a correlation matrix using corrplot and used the following code: cor.mtest <- function(mat, ...) { mat <- as.matrix(mat) n <- ncol(mat) p.mat<- matrix(NA, n, n) diag(p.mat) <...
more »

2017-08-06 19:08 (2) Answers