Recoding a variable to be a factor w/ level names in R

rename factor levels r
recode variables in r dplyr
recode numeric variables in r
recode multiple variables in r
as.factor in r
convert categorical variable to factor in r
assigning numbers to categorical variables in r
recode continuous variable into categorical r

Using my data-set bikeshare I want to recode the variable season to be a factor with meaningful level names (i.e "winter", "spring", "summer", "fall"), with spring as the baseline level.

Here is my attempt:

bikeshare <- bikeshare %>%
mutate(season = factor(c(1 = "winter", 
                      2 = "spring", 
                      3 = "summer", 
                      4 = "fall")))    

Here is the error I get:

Error in UseMethod("mutate_") : no applicable method for 'mutate_' applied 
to an object of class "factor"

I'd appreciate any help, I'm a beginner.

Here is an example of how to convert number (1:4) to factor ("winter", "spring", "summer", "fall"). The key is to use the factor function and set the levels and labels accordingly.

# Create example data frame
bikeshare <- data.frame(season = 1:4)

#   season
# 1      1
# 2      2
# 3      3
# 4      4


bikeshare2 <- bikeshare %>%
  mutate(season = factor(as.character(season), 
                         levels = c(1, 2, 3, 4),
                         labels = c("winter", "spring", "summer", "fall")))
#   season
# 1 winter
# 2 spring
# 3 summer
# 4   fall

Recode values — recode • dplyr, You can use recode() directly with factors; it will preserve the existing order of levels while See the forcats package for more tools for working with factors and their levels. When named, the argument names should be the current values to be recode(char_vec, � Recoding a categorical variable. The easiest way is to use revalue () or mapvalues () from the plyr package. This will code M as 1 and F as 2, and put it in a new column. Note that these functions preserves the type: if the input is a factor, the output will be a factor; and if the input is a character vector, the output will be a character vector.

To create your factor, you need to provide your data and your season labels to the factor() function.

However, since you want Spring to be the baseline level, I believe you will have to specify the levels and labels in a specific order - forcing spring to be first:

bikeshare <- data.frame(season = 1:4)

bikeshare$seasonfactor <- factor(x = bikeshare$season,
                                 levels = c(2,3,4,1), 
                                 labels = c("spring", "summer", "fall", "winter"))

#>  Factor w/ 4 levels "spring","summer",..: 4 1 2 3

#>   season seasonfactor
#> 1      1       winter
#> 2      2       spring
#> 3      3       summer
#> 4      4         fall

Created on 2019-03-03 by the reprex package (v0.2.1)

This is a tiny bit confusing, because in seasonfactor, Spring is represented by a 1, but in the original season it is represented by a 2.

Recode: Recode the Values of an Integer or Factor Variable in lessR , The values of the original variable may be overwritten with the recoded values, strings because that task is best accomplished with the standard R factor function . The name of the data frame from which to create the subset, which is d by default. recode integer values to levels of a factor with value labels # with the� Recode a Variable. Recodes a numeric vector, character vector, or factor according to simple recode specifications. Recode is an alias for recode that avoids name clashes with packages, such as Hmisc, that have a recode function.

Another alternative is to label the integer levels, with the help of the labelled package. So they remain integers but with the labels as metadata. If at any point you want to convert the labelled integers to factors, you can achieve that with the to_factor function.


labels <- c(
  "winter" = 1,
  "spring" = 2,
  "summer" = 3,
  "fall" = 4)

bikeshare <-
  tibble(season = 1:4) %>%
  mutate(season = labelled(season, labels)) %>%
  mutate(seasonF = to_factor(season))
#> # A tibble: 4 x 2
#>       season seasonF
#>    <int+lbl> <fct>  
#> 1 1 [winter] winter 
#> 2 2 [spring] spring 
#> 3 3 [summer] summer 
#> 4 4 [fall]   fall

Created on 2019-03-03 by the reprex package (v0.2.1)

Understanding Factors – Programming with R, How do I work with factors? For instance, if you have a factor with 2 levels: the as.numeric() function is still required to convert the values to the proper type� Note that that recode_factor function does not only replace the values, it also removes the old factor level and creates a new factor level. Video & Further Resources. Do you want to learn more about recoding data with the dplyr package in R? Then you may want to watch the following video of my YouTube channel.

Recoding data, Renaming levels of a factor for more information about can do the following with R's built-in functions:. Following is an example of factor in R. > x [1] single married married single Levels: married single Here, we can see that factor x has four elements and two levels. We can check if a variable is a factor or not using class() function. Similarly, levels of a factor can be checked using the levels() function.

Recode variables, Cut continuous variables into categorical variables. Free recoding of value ranges into categories; Turn ordered value ranges into factor levels using cut(). For more complicated criteria, use case_when (). You can use recode () directly with factors; it will preserve the existing order of levels while changing the values. Alternatively, you can use recode_factor (), which will change the order of levels to match the order of replacements.

Factor variables, Factor variables are categorical variables that can be either numeric or string variables. There are a number of advantages to converting categorical variables to factor variables. the new level, “very.high”, to the factor variable ses.f which we do by using the factor function with the levels argument. Your Name ( required). # Rename by name: change "beta" to "two" levels (x)[levels (x) == "beta"] <-"two" # You can also rename by position, but this is a bit dangerous if your data # can change in the future. If there is a change in the number or positions of # factor levels, then this can result in wrong data.