简体   繁体   中英

Recode dataframe using R

I have a dataframe that I am trying to recode. I have done something like this before, but my code no longer works. Since the last time, I have changed versions of R studio. I am trying to recode string variables (ie A, B, C, etc.) into numeric variables (ie 5, 4, 3, etc.). Here is an example dataframe:

DF
PreQ1    PreQ2   PreQ3    PreQ4    PostQ1    PostQ2 ... PostQ4
A          A       B         C        C         D         E
B          E       A         C        B         A         B
A          A       B         C        C         D         A

Recode so "A"= 5, "B"= 4, "C"=3,"D"= 2, "E"= 1

To get this:

DF.2
PreQ1    PreQ2   PreQ3    PreQ4    PostQ1    PostQ2 ... PostQ4
 5          5       4        3       3           2        1
 4          1       5        3       4           5        4
 5          5       4        3       3           2        5

I have tried different variations on the following code without success:

DF.2<-DF %>% 
mutate(across(where(as.character), ~ recode, 'A'= 5, 'B'= 4, 'C'=3,'D'= 2, 'E'= 1))

DF.2<-DF %>% 
mutate(across(“PreQ1”: “PostQ4”), recode, 'A'= 5, 'B'= 4, 'C'=3,'D'= 2, 'E'= 1))

DF.2<-DF %>% 
 mutate(across(c(“PreQ1”: “PostQ4”), recode, 'A'= 5, 'B'= 4, 'C'=3,'D'= 2, 'E'= 1))

Any help would be appreciated!

In base R , we create a named vector , loop over the columns of the dataset, use the named vector to match and replace and assign it back to the dataset

nm1 <- setNames(5:1, LETTERS[1:5])
DF[] <- lapply(DF, function(x) nm1[x])

data

DF <- structure(list(PreQ1 = c("A", "B", "A"), PreQ2 = c("A", "E", 
"A"), PreQ3 = c("B", "A", "B"), PreQ4 = c("C", "C", "C"), PostQ1 = c("C", 
"B", "C"), PostQ2 = c("D", "A", "D"), PostQ4 = c("E", "B", "A"
)), class = "data.frame", row.names = c(NA, -3L))

You can use -

library(dplyr)

DF %>% 
  mutate(across(PreQ1:PostQ4, recode, 'A'= 5, 'B'= 4, 'C'=3,'D'= 2, 'E'= 1))

#  PreQ1 PreQ2 PreQ3 PreQ4 PostQ1 PostQ2 PostQ4
#1     5     5     4     3      3      2      1
#2     4     1     5     3      4      5      4
#3     5     5     4     3      3      2      5

Or with a different syntax -

DF %>% 
  mutate(across(PreQ1:PostQ4, ~recode(., 'A'= 5, 'B'= 4, 'C'=3,'D'= 2, 'E'= 1)))

A base R option using match

df[] <- match(as.matrix(df), c("E", "D", "C", "B", "A"))

gives

> df
  PreQ1 PreQ2 PreQ3 PreQ4 PostQ1 PostQ2 PostQ4
1     5     5     4     3      3      2      1
2     4     1     5     3      4      5      4
3     5     5     4     3      3      2      5

Does this work:

library(dplyr)
library(tidyr)
df %>% pivot_longer(cols = everything()) %>% 
   mutate(value = case_when(value == 'A' ~ 5, value == 'B' ~ 4, value == 'C' ~ 3, value == 'D' ~ 2, TRUE ~ 1)) %>% 
     pivot_wider(names_from = name, values_from = value) %>% unnest(cols = everything())
# A tibble: 3 x 3
  PreQ1 PreQ2 PreQ3
  <dbl> <dbl> <dbl>
1     5     5     4
2     4     1     5
3     5     5     4

Data used:

df
  PreQ1 PreQ2 PreQ3
1     A     A     B
2     B     E     A
3     A     A     B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM