Using factor function to get rid of Xs in front of values and set the labels

34 views Asked by At

The years in the Year column have Xs in front of them. I would like to get rid of these, using the “factor” function and set the labels. How can i do this please?

enter image description here

The reason why i do this is to make my graph look prettier.

1

There are 1 answers

0
Isaiah On
library(stringr)
trendsdata <- structure(list(Year = c("X2015", "X2015", "X2015", "X2015", "X2015", 
                        "X2015")), row.names = c(NA, 6L), class = "data.frame")
#--------------
trendsdata$Year <- trendsdata$Year |> str_sub(2, 5) |>
  factor(labels = "First Year")
levels(trendsdata$Year)
    [1] "First Year"

Using str_sub to strip out the X, and factor to set the levels. I suspect that you will be happy without setting labels, in which case, just delete labels = "First Year"