简体   繁体   English

如何重新编码字符变量?

[英]How do I recode character variable?

I am a beginner in R so this is a very basic question. 我是R语言的初学者,所以这是一个非常基本的问题。 I do not find a specific answer to it so I would like to ask you here. 我没有找到具体的答案,所以我想在这里问你。

I'm confronted with the following challenge; 我面临以下挑战; I'd like to recode a character variable and create one out of this. 我想重新编码一个字符变量并从中创建一个。

Specifically, the variable in my data frame(data) is called "driver", with the categories "market", "legislation", "technology", and "mixed". 具体来说,我的数据框(数据)中的变量称为“驱动程序”,类别为“市场”,“立法”,“技术”和“混合”。

Now I would simply like to create a new variable, "driverrec", with the values "market" and "others". 现在,我只想创建一个新变量“ driverrec”,其值为“ market”和“ others”。 In "others" the three remaining variables shall be summarized. 在“其他”中,应总结其余三个变量。

I tried it with this page: http://rprogramming.net/recode-data-in-r/ 我在此页面上尝试过: http : //rprogramming.net/recode-data-in-r/

Basically, I tried the following code to adopt on mine, but it won't work for more than one category. 基本上,我尝试了以下代码在我的代码中采用,但不适用于多个类别。

#Create a new field called NewGrade
SchoolData$NewGrade <- recode(SchoolData$Grade,"5='Elementary'")

# my attempt
driverrec <- data$driver
recode(driverrec, "'Mixed'='others'") This is working.

But the whole recode is not working: 但是整个重新编码无法正常工作:

recode(driverrec, "'Mixed'='others'", "'Technology'='others'", 
"'Legislation'='others'", "'Market'='market'" ) 

I am looking forward to and thank you for your help. 我很期待并感谢您的帮助。

I found a solution not using the replace command: 我找到了不使用replace命令的解决方案:

data$driverrec[dataframe$driver=='Market'] <- 'market' data$driverrec[is.na(dataframe$driver)==TRUE] <- 'others' data $ driverrec [dataframe $ driver =='市场'] <-'市场'data $ driverrec [is.na(dataframe $ driver)== TRUE] <-'其他'

This worked fine; 效果很好; in order, someone is looking for a solution ;)! 为了有人,正在寻找解决方案;)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM