Add a new level to a factor and substitute existing one

后端 未结 3 944
傲寒
傲寒 2021-01-18 13:03

I\'m having a big trouble on dealing with levels names of a data frame.

I have a big data frame in which one of the colums is a factor with a LOT of levels.

相关标签:
3条回答
  • 2021-01-18 13:38

    Call make.names with unique = TRUE on your column.

    df$col_foo <- factor(make.names(df$col_foo, unique = TRUE))
    
    0 讨论(0)
  • 2021-01-18 13:46

    If you want all the entries to be unique then a factor does not gain you much over just using a character variable.

    Probably the simplest way to do what you want is to coerce to a character vector, use the duplicated function to find the duplicates and paste something onto the end of them, then if you want use factor to recoerce it back to a factor. Possibly something like:

    df$col_foo <- factor( ifelse( duplicated(df$col_fo), 
                        paste(df$col_foo, '_x', sep=''), as.character(df$col_foo)))
    
    0 讨论(0)
  • 2021-01-18 13:56

    You can edit the levels of the factor variable:

    levels(df$col_foo) <- c(levels(df$col_foo),"bar2_X","bar3_X")
    

    and then change the repeated levels to one of the new levels you added.

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