1
votes

I am a beginner in R so this is a very basic question. 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". In "others" the three remaining variables shall be summarized.

I tried it with this page: 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.

1
Which recode are you using? dplyr or arules? Not sure if they are the same. - NelsonGon
In the link, it is also showing the recode from car. So probably, you may need to add dplyr::recode or car::recode or else either of them may mask - akrun
The dplyr version would be dplyr::recode(names(iris), Species='Nope', Sepal.Length='Yep') - NelsonGon
I would suggest dealing with factors with forcats. Might be easier. - NelsonGon
Adding car:: or dplyr doesnt change anything This error appears: Error in if (as.factor) { : argument is not interpretable as logical I have no experience with forcats yet, I thought there would be an easy solution to change this, because it worked for a single category, but not for all 4... - dholzer

1 Answers

0
votes

I found a solution not using the replace command:

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

This worked fine; in order, someone is looking for a solution ;)!