r 重新编码(使用车库)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 重新编码(使用车库)相关的知识,希望对你有一定的参考价值。

# Install the car package
install.packages("car")

# Load the car package
library(car)
Now recode Grade from 5 to 6:

# Recode grade 5 to grade 6
SchoolData$Grade<-recode(SchoolData$Grade,"5=6")
If you want to recode based on text, use the ‘ mark around the text.

Now recode Grade from 5 to 6:

# Recode grade 5 to grade 6
SchoolData$Grade<-recode(SchoolData$Grade,"'Grade Five'=5")
To set recode multiple values use c()

# Recode grade 5 to grade 6
SchoolData$Grade<-recode(SchoolData$Grade,"c(1,2,3,4,5)='Five or Less'")
Recode can recode data into a new field. This code creates a new field called NewGrade based on Grade. Note that if you don’t specify that value is recoded R will just copy the existing value into the new field.

# Create a new field called NewGrade
SchoolData$NewGrade <- recode(SchoolData$Grade,"5='Elementary'")
Of course, you can convert a value to NA, or NA to a value.

# Recode grade 3 to NA
SchoolData$Grade<-recode(SchoolData$Grade,"3=NA")

# Or recode NA to 7
SchoolData$Grade <- recode(SchoolData$Grade,"NA=7")
One advantage to recode is that it can recode multiple values  in one line of code.

# Recode grade 5 to grade 6 and grade 6 to grade 7
SchoolData$Grade<-recode(SchoolData$Grade,"5=6;6=7")

以上是关于r 重新编码(使用车库)的主要内容,如果未能解决你的问题,请参考以下文章

R:dplyr 有条件地汇总并重新编码列中的值

在数据框架内重新编码李克特量表 - 为研究方法学习R的建议?

R:dplyr条件汇总并按列重新编码值

将 Stata 转换为 R - 重新编码

根据 R 中的一系列分类变量重新编码数值变量

r 用于抽取水合物的重新编码。动态cex和颜色仍然需要一些工作