如何将多个变量从字符串重新编码为数字?
How do I recode multiple variables from string to numeric?
我正在尝试将字符串变量(例如“None of the time”、“Some of the time”、“Often”...)中的多列数据重新编码为数值(例如“ None 次" = 0)。我已经看到许多对类似问题的不同回答,但是当我尝试这些时,他们似乎删除了所有数据并将其替换为 NA
.
For_Analysis <- data.frame(Q11_1=c("None of the time", "Often", "Sometimes"),
Q11_2=c("Sometimes", "Often", "Never"), Q11_3=c("Never", "Never", "Often"))
For_Analysis <- For_Analysis%>%
mutate_at(c("Q11_1", "Q11_2", "Q11_3"),
funs(recode(., "None of the time"=1, "Rarely"=2,
"Some of the time"=3, "Often"=4, "All of the time"=5)))
当我 运行 这第二位代码时,我得到以下输出
## There were 14 warnings (use warnings() to see them)
并且数据框中的所有数据都被重新编码为 NA
而不是我想要的数值。
您收到错误消息,因为有些值不匹配。您也可以将 mutate_at
替换为 across
.
library(dplyr)
For_Analysis <- For_Analysis%>%
mutate(across(starts_with('Q11'), ~recode(., "None of the time"=1, "Rarely"=2,
"Sometimes"=3, "Often"=4, "All of the time"=5, "Never" = 1)))
For_Analysis
# Q11_1 Q11_2 Q11_3
#1 1 3 1
#2 4 4 1
#3 3 1 4
我冒昧地假设 "Never"
与 "None of the time"
相同并编码为 1.
以下方法似乎对我的问题有效(将字符串变量重新编码为多列中的数字):
For_Analysis <- data.frame(Q11_1=c("Never", "Often", "Sometimes"),
Q11_2=c("Sometimes", "Often", "Never"), Q11_3=c("Never", "Never", "Often"))
New_Values <- c(1, 2, 3, 4, 5)
Old_Values <- unique(For_Analysis$Q11_1)
For_Analysis[1:3] <- as.data.frame(sapply(For_Analysis[1:3],
mapvalues, from = Old_Values, to = New_Values))
感谢您的帮助!
将其转换为 factor 类型的变量,然后再转换为数字的最简单方法。
library(tidyverse)
For_Analysis <- data.frame(Q11_1=c("None of the time", "Often", "Sometimes"),
Q11_2=c("Sometimes", "Often", "Never"), Q11_3=c("Never", "Never", "Often"))
fRecode = function(x) x %>% fct_inorder() %>% as.numeric()
For_Analysis %>% mutate_all(fRecode)
输出
Q11_1 Q11_2 Q11_3
1 1 1 1
2 2 2 1
3 3 3 2
我正在尝试将字符串变量(例如“None of the time”、“Some of the time”、“Often”...)中的多列数据重新编码为数值(例如“ None 次" = 0)。我已经看到许多对类似问题的不同回答,但是当我尝试这些时,他们似乎删除了所有数据并将其替换为 NA
.
For_Analysis <- data.frame(Q11_1=c("None of the time", "Often", "Sometimes"),
Q11_2=c("Sometimes", "Often", "Never"), Q11_3=c("Never", "Never", "Often"))
For_Analysis <- For_Analysis%>%
mutate_at(c("Q11_1", "Q11_2", "Q11_3"),
funs(recode(., "None of the time"=1, "Rarely"=2,
"Some of the time"=3, "Often"=4, "All of the time"=5)))
当我 运行 这第二位代码时,我得到以下输出
## There were 14 warnings (use warnings() to see them)
并且数据框中的所有数据都被重新编码为 NA
而不是我想要的数值。
您收到错误消息,因为有些值不匹配。您也可以将 mutate_at
替换为 across
.
library(dplyr)
For_Analysis <- For_Analysis%>%
mutate(across(starts_with('Q11'), ~recode(., "None of the time"=1, "Rarely"=2,
"Sometimes"=3, "Often"=4, "All of the time"=5, "Never" = 1)))
For_Analysis
# Q11_1 Q11_2 Q11_3
#1 1 3 1
#2 4 4 1
#3 3 1 4
我冒昧地假设 "Never"
与 "None of the time"
相同并编码为 1.
以下方法似乎对我的问题有效(将字符串变量重新编码为多列中的数字):
For_Analysis <- data.frame(Q11_1=c("Never", "Often", "Sometimes"),
Q11_2=c("Sometimes", "Often", "Never"), Q11_3=c("Never", "Never", "Often"))
New_Values <- c(1, 2, 3, 4, 5)
Old_Values <- unique(For_Analysis$Q11_1)
For_Analysis[1:3] <- as.data.frame(sapply(For_Analysis[1:3],
mapvalues, from = Old_Values, to = New_Values))
感谢您的帮助!
将其转换为 factor 类型的变量,然后再转换为数字的最简单方法。
library(tidyverse)
For_Analysis <- data.frame(Q11_1=c("None of the time", "Often", "Sometimes"),
Q11_2=c("Sometimes", "Often", "Never"), Q11_3=c("Never", "Never", "Often"))
fRecode = function(x) x %>% fct_inorder() %>% as.numeric()
For_Analysis %>% mutate_all(fRecode)
输出
Q11_1 Q11_2 Q11_3
1 1 1 1
2 2 2 1
3 3 3 2