I am new to R. Maybe this is very simple question. I have a dataframe, there is column that is factor. This factor has three level that 1,2,3. Now I want to change these level(1,2,3) to level(red,blue,dark). Does anybody how to do this job? Thank you very much Aimin
Aimin Yan wrote:> I am new to R. Maybe this is very simple question. > I have a dataframe, there is column that is factor. > This factor has three level that 1,2,3. > Now I want to change these level(1,2,3) to level(red,blue,dark). > Does anybody how to do this job?levels(dataframe$column) <- c("red", "blue", "dark") Uwe Ligges> Thank you very much > > Aimin > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Aimin Yan <aiminy <at> iastate.edu> writes:> I am new to R. Maybe this is very simple question. > I have a dataframe, there is column that is factor. > This factor has three level that 1,2,3. > Now I want to change these level(1,2,3) to level(red,blue,dark). > Does anybody how to do this job?levels() help page has entry about this. Try x <- factor(round(runif(n=10, min=1, max=3))) x [1] 3 1 1 3 1 2 2 2 1 2 Levels: 1 2 3 levels(x) <- list(red=c(1), blue=c(2), dark=c(3)) x [1] dark red red dark red blue blue blue red blue Levels: red blue dark You have factor in data.frame, so you have to use levels(myDF$x) <- ... Gregor