0

I made a data.table by:

p1 <- list(N=999) 
d = data.table(ID = 1:p1$N)
d[,Initial_Grouping := (1:.N - 1) %/% 333]

So I get for "ID 1:333", "Initial_Grouping" = 0; "ID 334:667", "Initial_Grouping" = 1; ID 667:999", "Initial_Grouping" = 2

Now, I would like to use the rnorm function and form a 3rd column "Size" which contains random variables for each of the "Initial_Grouping". I want each of the groups to have a different and specific mean and standard deviation.

One of the things I tried is this:

d[,Firm_Size := as.integer(exp((rnorm(333,mean=3,sd=1,by = (d$Initial_Grouping ==0))))),
             as.integer(exp((rnorm(333,mean=3,sd=1,by = (d$Initial_Grouping ==1))))),
             as.integer(exp((rnorm(333,mean=3,sd=1,by = (d$Initial_Grouping ==2)))))]

# Error in `[.data.table`(d, , `:=`(Size, as.integer(exp((rnorm(333,  : 
#   Provide either by= or keyby= but not both

1 Answer 1

1

Defining a lookup data.table with your parameters:

z <- data.table(Initial_Grouping = c(0, 1, 2), mn = c(1, 5, 8), sd = c(1, 2, 9)))
setkey(z, "Initial_Grouping")

d[, rnorm(.N, mean = z[.BY, mn], sd = z[.BY, mn]), by = Initial_Grouping]

     Initial_Grouping         V1
  1:                0  2.2026478
  2:                0 -0.8718570
  3:                0  2.5910559
  4:                0  1.7419309
  5:                0  1.5093134
 ---                            
995:                2 19.2724841
996:                2 24.4791871
997:                2  4.5289828
998:                2  6.4106569
999:                2 -0.7529038
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.