R: ifelse function returns vector position instead of value (string)

后端 未结 3 745
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 17:26

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

3条回答
  •  既然无缘
    2020-12-16 18:11

    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} .

提交回复
热议问题