MichaelChirico MichaelChirico - 2 months ago 5
R Question

Odd issue using .SD to create several percentage variables at once in an R data.table

I have a bunch of variables I'm trying to convert to percentages (stored as new variables) in a

data.table
; since it's not every column in the table, I'm using
.SD
&
.SDcols
to accomplish this quickly.

Here's the basics of what I tried that I thought would work:

pct_cols <- c("x","y","z") #s.t. x+y+z=total, for example
dt[ , paste0(pct_cols, "_pct") := .SD/total, .SDcols=pct_cols]


However, this spits at me:


Error in
eval(expr, envir, enclos)
: object
total
not found


However, when I make a slight adjustment to the second line, it works:

dt[ , paste(pct_cols, "pct", sep="_") := .SD/dt$total, .SDcols=pct_cols]


Any idea why the first version's not working?

Answer

Issue #495 is solved now with this recent commit, we can now do this just fine:

require(data.table) # v1.9.7+
dt = data.table(x=1:5, y=6:10, z=11:15, total=1:5 + 6:10 + 11:15)
pct_cols <- c("x", "y", "z") #s.t. x+y+z=total, for example
dt[ , paste0(pct_cols, "_pct") := .SD/total, .SDcols=pct_cols][]
#    x  y  z total      x_pct     y_pct     z_pct
# 1: 1  6 11    18 0.05555556 0.3333333 0.6111111
# 2: 2  7 12    21 0.09523810 0.3333333 0.5714286
# 3: 3  8 13    24 0.12500000 0.3333333 0.5416667
# 4: 4  9 14    27 0.14814815 0.3333333 0.5185185
# 5: 5 10 15    30 0.16666667 0.3333333 0.5000000
Comments