rk7
rk7

Reputation: 1

How do I create a column which uses certain functions based on another column?

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

Upvotes: 0

Views: 133

Answers (1)

s_baldur
s_baldur

Reputation: 33548

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

Upvotes: 1

Related Questions