简体   繁体   中英

what's the difference between these two command in R?(levels of factor variable)

I'm new to R, I was trying to figure out what is the difference between the last two commands?Are they the same?Because the output are exact the same.

yesno <- sample(c("yes", "no"),size = 10, replace = TRUE);yesno
> yesno
[1] "yes" "yes" "no"  "yes" "yes" "yes" "no"  "yes" "no"  "yes"

yesnofac <- factor(yesno, levels = c("yes", "no"));yesnofac
[1] yes yes no  yes yes yes no  yes no  yes
Levels: yes no

relevel(yesnofac, ref = "yes")   #Reorder Levels of Factor
[1] yes yes no  yes yes yes no  yes no  yes
Levels: yes no

In the above example, there is no difference as the first level is 'yes'. But, if we change the order of levels , then the relevel willl make the 'yes' as the first level

yesnofac <- factor(yesno, levels = c("no", "yes"))
yesnofac
#[1] yes no  yes yes yes yes yes no  yes yes
# Levels: no yes

relyesnofac <- relevel(yesnofac, ref = "yes")
relyesnofac
#[1] yes no  yes yes yes yes yes no  yes yes
# Levels: yes no'

Check the levels , it is now different in order

levels(relyesnofac)
#[1] "yes" "no" 
levels(yesnofac)
#[1] "no"  "yes"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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