I have a very strange problem concerning the ifelse function: it does not return a factor (as I want) but something like the position of the factor.
The dataset I u
Modify your ifelse
as follows
aDDs$top <- ifelse(
aDDs$answer %in% temp, ## condition: match aDDs$answer with row.names in summary df
levels(aDDs$answer)[aDDs$answer], ## then it should be named as aDDs$answer **THIS IS THE PROBLEM**
"Other" ## else it should be named "Other"
)
Notice the function levels
and the box brackets. Levels knows how many factors are their and their index. So, essentially what we are saying is give me the the factor value corresponding to some index value.
Sample demo:
topCountries<-as.factor(c("India", "USA", "UK"))
AllCountries<-as.factor(c("India", "USA", "UK", "China", "Brazil"))
myData<-data.frame(AllCountries)
myData
myData$top<-ifelse(
myData$AllCountries %in% topCountries,
levels(myData$AllCountries)[myData$AllCountries],
"Other"
)
myData
the top
column in myData will have "Other" for China & Brazil. For rows where Allcountries in {India, USA, UK} it will return their respective values i.e., {India, USA, UK} . Without the use of levels
it will return "Other" and factor index values for {India, USA, UK} .