如何简化转换几个列的属性和重新编码多个列?

问题描述

我有几行代码正在研究如何简化。我这样做的尝试导致了错误。以下是几行代码:

SS_data$Cope1 <- as.numeric(SS_data$Cope1)
SS_data$Cope2 <- as.numeric(SS_data$Cope2)
SS_data$Cope3 <- as.numeric(SS_data$Cope3)
SS_data$Cope4 <- as.numeric(SS_data$Cope4)
SS_data$Cope5 <- as.numeric(SS_data$Cope5)
SS_data$Cope6 <- as.numeric(SS_data$Cope6)
SS_data$Cope7 <- as.numeric(SS_data$Cope7)
SS_data$Cope8 <- as.numeric(SS_data$Cope8)
SS_data$Cope9 <- as.numeric(SS_data$Cope9)
SS_data$Cope10 <- as.numeric(SS_data$Cope10)
SS_data$Cope11 <- as.numeric(SS_data$Cope11)
SS_data$Cope12 <- as.numeric(SS_data$Cope12)
SS_data$Cope13 <- as.numeric(SS_data$Cope13)
SS_data$Cope14 <- as.numeric(SS_data$Cope14)
SS_data$Cope15 <- as.numeric(SS_data$Cope15)
SS_data$Cope16 <- as.numeric(SS_data$Cope16)
SS_data$Cope17 <- as.numeric(SS_data$Cope17)
SS_data$Cope18 <- as.numeric(SS_data$Cope18)
SS_data$Cope19 <- as.numeric(SS_data$Cope19)
SS_data$Cope20 <- as.numeric(SS_data$Cope20)

我也在尝试简化下面的代码。我最终为每个变量重新编码,我想知道是否还有一种方法可以简化此操作。

WHOQOL16[WHOQOL16 == "Very dissatisfied"] <- 1
WHOQOL16[WHOQOL16 == "Dissatisfied"] <- 2
WHOQOL16[WHOQOL16 == "Neither satisfied nor dissatisfied"] <- 3
WHOQOL16[WHOQOL16 == "Satisfied"] <- 4
WHOQOL16[WHOQOL16 == "Very satisfied"] <- 5
              
WHOQOL17[WHOQOL17 == "Very dissatisfied"] <- 1
WHOQOL17[WHOQOL17 == "Dissatisfied"] <- 2
WHOQOL17[WHOQOL17 == "Neither satisfied nor dissatisfied"] <- 3
WHOQOL17[WHOQOL17 == "Satisfied"] <- 4
WHOQOL17[WHOQOL17 == "Very satisfied"] <- 5
              
WHOQOL18[WHOQOL18 == "Very dissatisfied"] <- 1
WHOQOL18[WHOQOL18 == "Dissatisfied"] <- 2
WHOQOL18[WHOQOL18 == "Neither satisfied nor dissatisfied"] <- 3
WHOQOL18[WHOQOL18 == "Satisfied"] <- 4
WHOQOL18[WHOQOL18 == "Very satisfied"] <- 5
              
WHOQOL19[WHOQOL19 == "Very dissatisfied"] <- 1
WHOQOL19[WHOQOL19 == "Dissatisfied"] <- 2
WHOQOL19[WHOQOL19 == "Neither satisfied nor dissatisfied"] <- 3
WHOQOL19[WHOQOL19 == "Satisfied"] <- 4
WHOQOL19[WHOQOL19 == "Very satisfied"] <- 5

解决方法

张贴到标签上的问题应包括可复制的数据,但我已经这样做了 这次您将在注释末尾。

以下仅使用基数R。

如果您想从头开始再次运行代码,请先在DF中复制DF2,因为代码将覆盖DF2

下一步将第1列和第2列转换为数字,并将第3列和第4列中的X,Y和Z转换为1、2和3。如果第1列或第2列中出现非数字条目,或者不是X,Y或X列的条目Z出现在第3列或第4列中,然后将NA分配给这些条目。 (或者在第二行代码中,在dplyr软件包中存在一个recode函数,在汽车软件包中存在一个具有相同目的的不同recode函数。)

在此示例中,列号是显而易见的,但如果不在您的数据中,请使用grep("Cope",names(DF))之类的表达式来获取它们。

DF2 <- DF
DF2[1:2] <- lapply(DF2[1:2],as.numeric)
DF2[3:4] <- lapply(DF2[3:4],match,c("X","Y","Z"))

给出以下警告只是为了让您知道它遇到了无法转换为数字的值,因此将其转换为NA。

> DF2
Warning message:
In lapply(DF[1:2],as.numeric) : NAs introduced by coercion
   A  B  C  D
1  1 11  1  1
2 NA 12  2 NA
3  3 13 NA  3

注意

DF <- data.frame(A = c("1","x","3"),B = c("11","12","13"),C = c("X","a"),D = c("X",NA,"Z"))
,

dplyr中,您可以使用across函数将同一函数应用于多个列。

我们将以"Cope"开头的列更改为数字,并重新编码以"WHOQOL"开头的列。

library(dplyr)

SS_data_new <- SS_data %>% 
                    mutate(across(starts_with('Cope'),as.numeric),across(starts_with('WHOQOL'),~recode(.,"Very dissatisfied" = 1,"Dissatisfied" = 2,"Neither satisfied nor dissatisfied" = 3,"Satisfied" = 4,"Very satisfied" = 5)))
SS_data_new
#  Cope1 Cope2 WHOQOL
#1     1     4      1
#2     2     5      1
#3     3     6      4
str(SS_data_new)
#data.frame':   3 obs. of  3 variables:
# $ Cope1 : num  1 2 3
# $ Cope2 : num  4 5 6
# $ WHOQOL: num  1 1 4

数据

SS_data <- data.frame(Cope1 = c('1','2','3'),Cope2 = c('4','5','6'),WHOQOL = c("Very dissatisfied","Very dissatisfied","Satisfied"))

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...