bvowe
bvowe

Reputation: 3394

R DataTable Function For Select Columns

 set.seed(1)
   data=data.frame(
    student=1:5000,
    alfa =runif(5000), 
    bravo =runif(5000), 
    charlie =runif(5000), 
    delta =runif(5000), 
    echo =runif(5000))
setDT(data)
data[, colnames(data[1:3]) := fifelse(colname(data) < 2500, "a", "b")]

So basically what I wish to do is: For columns 1-3 (student, alfa, bravo) I wish to replace values < 2500 with "a" or if it is 2500 and more than replace it with "b". I wish to do this simultaneously for the three columns. I know I can write separate commands with := fifelse() but the thing is I have a much bigger data and would need to do this about 100 times!!

Upvotes: 1

Views: 36

Answers (1)

akrun
akrun

Reputation: 887851

We can specify the columns in .SDcols, loop over the .SD with lapply, apply the fifelse , assign (:=) the output back to the columns

library(data.table)
nm1 <- names(data)[1:3]
data[, (nm1) := lapply(.SD, function(x) fifelse(x < 2500, "a", "b")),
       .SDcols = nm1]

-output

head(data)
#   student alfa bravo    charlie     delta      echo
#1:       a    a     a 0.06471249 0.2133639 0.2106027
#2:       a    a     a 0.67661240 0.9693570 0.1147864
#3:       a    a     a 0.73537169 0.7277707 0.1453641
#4:       a    a     a 0.11129967 0.3439635 0.3099322
#5:       a    a     a 0.04665462 0.1440118 0.1502421
#6:       a    a     a 0.13091031 0.1385575 0.5266817

Upvotes: 1

Related Questions