Replace contents of factor column in R dataframe

后端 未结 8 1682
我在风中等你
我在风中等你 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:39

    I bet the problem is when you are trying to replace values with a new one, one that is not currently part of the existing factor's levels:

    levels(iris$Species)
    # [1] "setosa"     "versicolor" "virginica" 
    

    Your example was bad, this works:

    iris$Species[iris$Species == 'virginica'] <- 'setosa'
    

    This is what more likely creates the problem you were seeing with your own data:

    iris$Species[iris$Species == 'virginica'] <- 'new.species'
    # Warning message:
    # In `[<-.factor`(`*tmp*`, iris$Species == "virginica", value = c(1L,  :
    #   invalid factor level, NAs generated
    

    It will work if you first increase your factor levels:

    levels(iris$Species) <- c(levels(iris$Species), "new.species")
    iris$Species[iris$Species == 'virginica'] <- 'new.species'
    

提交回复
热议问题