How to change factor labels into string in a data frame

前端 未结 3 1756
挽巷
挽巷 2020-12-17 15:50

I have the following data frame:

    name1  name2
        A      B
        B      D
        C      C
        D      A

the columns \"name1\"

3条回答
  •  一向
    一向 (楼主)
    2020-12-17 16:13

    you're looking for as.character, which you need to apply to each column of the data.frame

    Assuming X is your data.frame
    If fctr.cols are the names of your factor columns, then you can use:

     X[, fctr.cols] <- sapply(X[, fctr.cols], as.character)
    

    You can collect your factor columns using is.factor:

     fctr.cols <- sapply(X, is.factor)
    

提交回复
热议问题