Using `car` to recalculate a range of columns

I spend on the Internet and cannot figure out how to apply carto recalculate values ​​for a range of columns.

To recode the values ​​for one column, I would execute a command, for example:

 df$dv_r <- recode(df$dv, "2=1;1=0;0=NA")

And if I want to do this for all data.frame, I can run:

 df_2 <- lapply(df, FUN = function(x) recode(x, "2=1;1=0;0=NA"))

However, I am not sure how to do this for a number of columns - for example, in a hypothetical column data.tablecalled df, how would I recode values ​​for columns in the range from 20:40?

Thank! Of course, this is very easy for R experts.

+7
source share
3 answers

There may be a better data.tableway to do this, but there is one possibility:

library(data.table)
library(car)

## Here is some sample data
set.seed(1)
dt <- data.table(A = sample(0:2, 10, replace = TRUE), 
                 B = sample(0:2, 10, replace = TRUE),
                 C = sample(0:2, 10, replace = TRUE),
                 D = rnorm(10), E = rnorm(10), ID = 1:10)
dt
#     A B C           D           E ID
#  1: 0 0 2 -0.04493361 -0.05612874  1
#  2: 1 0 0 -0.01619026 -0.15579551  2
#  3: 1 2 1  0.94383621 -1.47075238  3
#  4: 2 1 0  0.82122120 -0.47815006  4
#  5: 0 2 0  0.59390132  0.41794156  5
#  6: 2 1 1  0.91897737  1.35867955  6
#  7: 2 2 0  0.78213630 -0.10278773  7
#  8: 1 2 1  0.07456498  0.38767161  8
#  9: 1 1 2 -1.98935170 -0.05380504  9
# 10: 0 2 1  0.61982575 -1.37705956 10

.SDcols, , .

dt[, 1:3 := lapply(.SD, recode, "2=1;1=0;0=NA"), .SDcols = 1:3]
dt
#      A  B  C           D           E ID
#  1: NA NA  1 -0.04493361 -0.05612874  1
#  2:  0 NA NA -0.01619026 -0.15579551  2
#  3:  0  1  0  0.94383621 -1.47075238  3
#  4:  1  0 NA  0.82122120 -0.47815006  4
#  5: NA  1 NA  0.59390132  0.41794156  5
#  6:  1  0  0  0.91897737  1.35867955  6
#  7:  1  1 NA  0.78213630 -0.10278773  7
#  8:  0  1  0  0.07456498  0.38767161  8
#  9:  0  0  1 -1.98935170 -0.05380504  9
# 10: NA  1  0  0.61982575 -1.37705956 10
+6

. data.frame data.frame:

  df_2[ , col_names]  <- lapply(df[ ,colnames] , 
                                FUN = function(x) recode(x, "2=1;1=0;0=NA"))

col-number:

  df_2[ , 20:40]  <- lapply(df[ ,20:40] , 
                                FUN = function(x) recode(x, "2=1;1=0;0=NA"))
+2

Using variable names instead of column numbers will look something like this:

cnames <- c("A", "B", "C")    
dt[ , (cnames) := lapply(.SD, recode, "2=1;1=0;0=NA"), .SDcols = cnames]
+1
source

All Articles