[R] factor with numeric names
David Winsemius
dwinsemius at comcast.net
Fri Mar 25 15:18:13 CET 2011
On Mar 25, 2011, at 8:30 AM, agent dunham wrote:
> Dear all,
>
> According to the post I was trying:
>
> factorA = c(2,2,3,3,4,4,3,4,2,2)
> levels(factorA <- c("lv1","lv2","lv3") )
Well, this is wrong. Try:
levels(factorA) <- c("lv1","lv2","lv3")
> factorA
[1] 2 2 3 3 4 4 3 4 2 2
attr(,"levels")
[1] "lv1" "lv2" "lv3"
> But this returns NULL and doesn't change factor names.
>
> Actually, my factor is included in a data.frame, so I also tried:
>
> levels(df$factorA)[levels(df$factorA)=="2"] <- "lv1" Also
> levels(df$factorA)[levels(df$factorA)==2] <- "lv1"
> levels(df$factorA)[levels(df$factorA)=="3"] <- "lv2"
> levels(df$factorA)[levels(df$factorA)=="4"] <- "lv3"
>
> then I type table(df$factorA) and it doesn't work either.
>
> After attach(df), I tried also this
>
> for(i in 1:10)
> { if(factor(factorA)[i]==2) factorA[i]="lv2" else {;
> if(factor(factorA)[i]==3) factorA[i]="lv3" else {;
> factorA[i]= "lv4"}}}}
That looks painful. What book or resource are you using?
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list