Replace contents of factor column in R dataframe

后端 未结 8 1664
我在风中等你
我在风中等你 2020-11-28 20:59

I need to replace the levels of a factor column in a dataframe. Using the iris dataset as an example, how would I replace any cells which contain virginic

8条回答
  •  借酒劲吻你
    2020-11-28 21:38

    Using dlpyr::mutate and forcats::fct_recode:

    library(dplyr)
    library(forcats)
    
    iris <- iris %>%  
      mutate(Species = fct_recode(Species,
        "Virginica" = "virginica",
        "Versicolor" = "versicolor"
      )) 
    
    iris %>% 
      count(Species)
    
    # A tibble: 3 x 2
         Species     n
           
    1     setosa    50
    2 Versicolor    50
    3  Virginica    50   
    

提交回复
热议问题