Home > Enterprise >  Different random numbers when two conditions are met in R
Different random numbers when two conditions are met in R

Time:08-18

I have a data frame of three columns Distance, Age, and Value where there are three repeated Value for every Distance and Age combination. I would like to generate a random number for Value for certain Distance and Age combinations. I can get a random number to generate however, it is the same random number repeated and I need three different random numbers.

Example Data

set.seed(321)
dat <- data.frame(matrix(ncol = 3, nrow = 27))
colnames(dat)[1:3] <- c("Distance", "Age", "Value")
dat$Distance <- rep(c(0.5,1.5,2.5), each = 9)
dat$Age <- rep(1:3, times = 9)

The code below creates a random number for the Distance and Age combo but the random number is the same for each of the three measurements, they should be different random numbers.

dat$Value <- ifelse(dat$Distance == '0.5' & dat$Age == '1',
                                rep(rnorm(3,10,2),3), NA)

Instead of getting the same repeated random number for the Distance and Age combo

head(dat)
  Distance Age    Value
1      0.5   1 13.40981
2      0.5   2       NA
3      0.5   3       NA
4      0.5   1 13.40981
5      0.5   2       NA
6      0.5   3       NA

I would like different random numbers for the Distance and Age combo

head(dat)
  Distance Age    Value
1      0.5   1 13.40981
2      0.5   2       NA
3      0.5   3       NA
4      0.5   1 11.18246
5      0.5   2       NA
6      0.5   3       NA

The numbers for Value don't really matter and are for demonstration purposes only.

CodePudding user response:

Replace rep(rnorm(3,10,2),3) with rnorm(nrow(dat), 10, 2).

CodePudding user response:

Something like this?

library(dplyr)
dat %>%  
  mutate(Value = ifelse(Distance == 0.5 & Age == 1, sample(1000,nrow(dat), replace = TRUE), NA))
   Distance Age Value
1       0.5   1   478
2       0.5   2    NA
3       0.5   3    NA
4       0.5   1   707
5       0.5   2    NA
6       0.5   3    NA
7       0.5   1   653
8       0.5   2    NA
9       0.5   3    NA
10      1.5   1    NA
11      1.5   2    NA
12      1.5   3    NA
13      1.5   1    NA
14      1.5   2    NA
15      1.5   3    NA
16      1.5   1    NA
17      1.5   2    NA
18      1.5   3    NA
19      2.5   1    NA
20      2.5   2    NA
21      2.5   3    NA
22      2.5   1    NA
23      2.5   2    NA
24      2.5   3    NA
25      2.5   1    NA
26      2.5   2    NA
27      2.5   3    NA

CodePudding user response:

You can eliminate the ifelse():

idx <- dat$Distance == '0.5' & dat$Age == '1'
dat$Value[idx] <- rnorm(sum(idx), 10, 2)
head(dat)
head(dat, 7)
#   Distance Age    Value
# 1      0.5   1 10.91214
# 2      0.5   2       NA
# 3      0.5   3       NA
# 4      0.5   1 10.84067
# 5      0.5   2       NA
# 6      0.5   3       NA
# 7      0.5   1 11.15517
  •  Tags:  
  • r
  • Related