[R] ifelse function
Min (Tilda) Zhang
mzhang3 at ncsu.edu
Wed Nov 28 04:45:13 CET 2007
Hi there,
I need help with IFELSE function.
The column g of my dataset pth, pth$g consists of "aa", "ao", "dcl", "iy",
"sh".
The last few values of pth$g looks like:
[4496] sh ao ao sh iy dcl dcl aa iy iy aa sh ao ao
Levels: aa ao dcl iy sh
I want to convert these values into 1,2,3,4,5. I tried to use a loop and I
found the following statement did not work.
> pth$g[1]<-ifelse(pth$g[1]=="aa",1,pth$g[1])
Warning message:
invalid factor level, NAs generated in: `[<-.factor`(`*tmp*`, 1, value = 5L)
Then I tried this statement. It works, but I do not understand how it
convert the values not only into 1 but also into 2,3,4,5 at the same time.
> pth$g<-ifelse(pth$g=="aa",1,pth$g)
> pth$g
[1] 4 4 3 3 1 4 1 5 ....
I appreciate your help.
Min (Tilda) Zhang
Department of Statistics
North Carolina State University
More information about the R-help
mailing list