I have to use the "ifelse" function in R to uncode values used in a variable.
The data frame being used is dance. The variable is Type. Via comments I have received, here is what I have:
ifelse(dance$Type=="Swg","Swing",
ifelse(dance$Type=="Ldy","Lindy",
ifelse(dance$Type=="Blue","Blues",
else(dance$Type=="Contra","Contra"))))
I keep getting error messages. Are all of the commas correct? Also, did I end it correctly?
I keep getting error messages plus I'm supposed to specify the data frame I'm using somehow.
Any help would be greatly appreciated. Thank you.
If Type
is a factor then you can rename the levels and extract them in this way (using f
below):
f <- factor(c("Swg", "Ldy", "Blue", "Swg"))
# See the order of the levels by printing f. It's alphabetical
levels(f) <- c("Blues", "Lindy", "Swing")
as.character(f)
This will give
> f <- factor(c("Swg", "Ldy", "Blue", "Swg"))
> f
[1] Swg Ldy Blue Swg
Levels: Blue Ldy Swg
> as.character(f)
[1] "Swg" "Ldy" "Blue" "Swg"
> levels(f) <- c("Blues", "Lindy", "Swing")
> as.character(f)
[1] "Swing" "Lindy" "Blues" "Swing"
Thanks for your help. I figured it out via tips that were mentioned. I needed to name a dataframe and give an alternate solution for data not changed. I came up with:
Dance$new<-ifelse(dance$Type=="Swg","Swing",
ifelse(dance$Type=="Ldy","Lindy",
ifelse(dance$Type=="Blue","Blues",
ifelse(dance$Type=="Contra","Contra",F))))
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.