How to change factor labels into string in a data frame

前端 未结 3 1736
挽巷
挽巷 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:10

    This may be a little simpler than the answer above.

    #where your dataframe = df
    df.name1 <- as.character (df.name1)
    df.name2 <- as.character (df.name2)
    

    I need to do things like this all the time at work because the data is so messy. I have been able to do it on import with StringsAsFactors=FALSE, but in the newest version of r I am getting an error on read.csv. Ideally I will figure that out soon... In the meantime I have been doing this as a quick and effective method. It takes the old variable, foo, which is factor type, and converts it to a new variable, fooChar, which is character type. I usually do it in situ by naming the new variable the same as the old one, but you may want to play with it before you trust it to replace values.

    #Convert from Factor to Char
    #Data frame named data
    #Old Variable named foo, factor type
    #New Variable named fooChar, character type
    
    data$fooChar <-as.character(data$foo)
    
    #confirm the data looks the same:
    table (data$fooChar)
    
    #confirm structure of new variable
    str(data)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-17 16:17

    If you want to convert only the selected column of factor variable instead of all the factor variable columns in the data frame, you can use:

    file1[,n] <- sapply(file1[,n], as.character)
    

    where n is the column number.

    0 讨论(0)
提交回复
热议问题