Home > Mobile >  mutate and truncate functions in r not producing desired output
mutate and truncate functions in r not producing desired output

Time:12-07

I have some date data in the format Start_year = 2018/19, 2019/20, 2020/21 etc. I want to put it in the format 2018, 2019, 2020, as integers, for a group by clause later in the code.

select_data <- data %>%
  select(Product, Start_year, Number, Amount) %>%
  mutate(Avg_paid = Amount/Number)%>% #This works fine
  mutate(Start_year_short = as.integer(str_trunc(Start_year, 4, c("left"))))

The error message I get is:

Problem with `mutate()` column `Start_year_short`. `Start_year_short = as.integer(str_trunc(Start_year, 4, c("left")))`. NAs introduced by coercion

If I take the mutate out and do

Start_year <- as.integer(str_trunc(Start_year, 4, c("left")))

I get an object not found error instead. I really can't work out what's going wrong.

CodePudding user response:

How about this simpler truncation method:

data.frame(Start_year) %>%
  mutate(Start_year_short = str_replace(Start_year, "(\\d ).*", "\\1"))

With conversion to integer:

data.frame(Start_year) %>%
  mutate(Start_year_short = as.integer(str_replace(Start_year, "(\\d ).*", "\\1")))

CodePudding user response:

Use this instead as.integer(substr(Start_year, 1, 4))

CodePudding user response:

I would use stringr::str_split instead to be more flexible with the length of the numbers.

Start_year = c("2018/19", "2019/20", "2020/21") 
Start_year <- as.integer(stringr::str_split(Start_year, pattern="/"))
  • Related