lundi 26 juin 2017

How to keep certain factors while changing others with the ifelse() function in R?

I am very new to programming in R and I am still learning a lot, so my question might sound a bit foolish for the more advanced programmers of you. At the moment I have problems with the ifelse() function in R.

My column (lets call it A) I have problems with looks like this:

irelevant
irelevant
2
6
very important
5
very important
irelevant
4

Because of that I wanted to use a iflese funtion to turn the "very important" into a 7, the "irelevant" into a 1 and keep the other factors. So i tried this;

Data$B <- ifelse(Data$A == "irelevant", 1,
                        ifelse(Data$A == "very important", 7, Data$A))

Unfortunately it didn´t work and strangely it returned my column like this:

1
1
3
7
7
6
7
1
5

The function converted "irelevant" and "very important" correctly but added +1 to all other factors. How can I avoid that or improve my function? I would be really glad if you could help me with that. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire