微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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"))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。