0

I am curious if I can do a similar thing as this question Apply a function to every specified column in a data.table and update by reference but creating new columns based off a group.

This is my code for example (very rudimentary code)

dt <- data.table(a = 1:4, b = 1:4, d = c("Hi", "Hello", "Please", "Hi"))

cols <- c("a", "b")
out_cols = paste("Percentile Rank 1", cols, sep = " ")
have.fun3[, c(out_cols) := lapply(.SD,  percent.rank.V2), .SDcols = cols]

Where I am calculating the Percentile rank of of columns a and b. My code it is setup to create two new columns called "Percentile Rank 1 a" and "Percentile Rank 1 b". However, what I would like to do is calculate the percentile rank by group and then create columns based off those groups.

So I would create 4 new columns called "Percentile Rank 1 a Hi" ... "Percentile Rank 2 a Hello" ... "Percentile Rank 2 b Please"

cols <- c("a", "b")
out_cols = paste("Percentile Rank 1", cols, sep = " ")
have.fun3[, c(out_cols) := lapply(.SD,  percent.rank.V2), .SDcols = cols, by = d]

but it doesn't do what I would like (it only outputs the Percentile Ranks for the Hi group).

Phil
  • 5,491
  • 3
  • 26
  • 61

0 Answers0