I'm trying to loop through columns 10 - 12 of the following data set df2, replacing 1 with a.2. with b. 3 with c and 4 with d
I've figured out how to perform the substitution on a single column using the code below.
df2[df2$DX == 1,]$DX <-'a'
df2[df2$DX == 2,]$DX <-'b'
df2[df2$DX == 3,]$DX <- 'c'
df2[df2$DX == 4,]$DX <- 'd'
df2$DX <- as.factor(df2$DX)
I'm not sure how to set up the for loop to loop over the other two columns. Thank you for your help.
CodePudding user response:
library(dplyr)
df2 <- data.frame(replicate(30,sample(0:10,1000,rep=TRUE)))
df3 <- df2 %>% mutate(across(10:12, ~ case_when(
.x == 1 ~ "a",
.x == 2 ~ "b",
.x == 3 ~ "c",
.x == 4 ~ "d",
T ~ as.character(.x)
)))
> head(df2)
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13
1 7 3 0 6 2 0 3 4 3 1 6 1 1
2 0 3 7 2 7 2 1 2 6 0 3 4 10
3 4 3 1 8 0 7 6 3 5 5 9 8 5
4 8 0 7 4 0 10 8 3 8 3 9 7 4
5 7 4 9 3 2 3 0 8 9 1 2 3 4
6 8 1 2 2 7 6 3 9 4 1 2 10 10
X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24
1 2 3 4 6 9 1 4 7 1 6 0
2 9 0 1 6 9 8 7 2 5 2 5
3 9 3 2 8 8 6 10 5 0 2 2
4 3 5 8 7 9 0 9 6 6 2 6
5 2 4 6 7 0 5 4 6 9 0 4
6 3 0 4 10 8 3 10 8 0 3 1
X25 X26 X27 X28 X29 X30
1 8 5 3 5 3 2
2 10 1 2 5 8 2
3 3 2 10 7 9 5
4 7 6 0 7 5 10
5 6 7 7 5 2 10
6 9 8 9 6 6 5
> head(df3)
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13
1 7 3 0 6 2 0 3 4 3 a 6 a 1
2 0 3 7 2 7 2 1 2 6 0 c d 10
3 4 3 1 8 0 7 6 3 5 5 9 8 5
4 8 0 7 4 0 10 8 3 8 c 9 7 4
5 7 4 9 3 2 3 0 8 9 a b c 4
6 8 1 2 2 7 6 3 9 4 a b 10 10
X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24
1 2 3 4 6 9 1 4 7 1 6 0
2 9 0 1 6 9 8 7 2 5 2 5
3 9 3 2 8 8 6 10 5 0 2 2
4 3 5 8 7 9 0 9 6 6 2 6
5 2 4 6 7 0 5 4 6 9 0 4
6 3 0 4 10 8 3 10 8 0 3 1
X25 X26 X27 X28 X29 X30
1 8 5 3 5 3 2
2 10 1 2 5 8 2
3 3 2 10 7 9 5
4 7 6 0 7 5 10
5 6 7 7 5 2 10
6 9 8 9 6 6 5
CodePudding user response:
library(dplyr)
mutate(df2, across(c(col10, col11, col12), ~factor(case_when(
. == 1 ~ "a",
. == 2 ~ "b",
. == 3 ~ "c",
. == 4 ~ "d",
TRUE ~ as.character(.),
))))
CodePudding user response:
Here's a base R solution:
df2[10:12] <- lapply( df2[10:12], function( x) { ifelse(x %in% 1:4, letters[x], x) })
For each column run ifelse
on the column vector of numerics and return "a":"d" ( = letters[1:4]
)when x is in the range 1:4, else return the original value. Necessarily the values outside 1:4 will still get converted to character values rather than staying numeric class..