[英]Recode dataframe using R
I have a dataframe that I am trying to recode.我有一个 dataframe 正在尝试重新编码。 I have done something like this before, but my code no longer works.我以前做过类似的事情,但我的代码不再有效。 Since the last time, I have changed versions of R studio.自上次以来,我已经更改了 R studio 的版本。 I am trying to recode string variables (ie A, B, C, etc.) into numeric variables (ie 5, 4, 3, etc.).我正在尝试将字符串变量(即 A、B、C 等)重新编码为数值变量(即 5、4、3 等)。 Here is an example dataframe:这是一个示例 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重新编码,“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在base R
中,我们创建了一个命名vector
,遍历数据集的列,使用命名向量进行匹配和替换并将其分配回数据集
nm1 <- setNames(5:1, LETTERS[1:5])
DF[] <- lapply(DF, function(x) nm1[x])
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
使用match
的基本 R 选项
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.