rename the level of a catgorical variable

Hallo R Studio Community,
I am having a strange issue:
not receveing any error by running the following code,

' data$dgn[data$dgn == "AHN"] <- "ALF" '

it comes out that the level is not changed and the new name desired is not set.

I tried to change the class of the variable from character to factor, and nothing changed.

Any suggestion you can give me? Thanks!!


it should work if "AHN" is really in your data:

dgn <- letters[1:10]
dgn[dgn=="c"] <- "C" # replacement done
dgn[dgn=="z"] <- "Z" # no replacement done, since "z" is not in dgn :(


I think you got this issue because your variable is a factor and "ALF" is not a level of the variable.
You can try this for exemple:

#convert your variable into Character
#run your changes 
data$dgn[data$dgn == "AHN"] <- "ALF"
#Then reconvert the variable into factor




You can also try this:

levels(data$dgn)<-list("AHN"="ALF",      "a"="a",    "b"="b",  ......,  "x"="x")

you are right! there was a comma I did not see, now I am fixing the issue. Thanks!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.