How to change the class of a variable inside a table / data frame / tibble?

2

I have a table called tab01 with the following variables (columns) and their respective classes in parentheses: uf (character), regiao (character), ano (double) and pop (double ).

I want, inside the table:
1) transform the variable region into the class, factor and 2) inform R that the year variable corresponds to a "date"

So, how do I do this using the base R? And using the tidyverse ecosystem?

    
asked by anonymous 27.06.2017 / 21:03

1 answer

4

First would be good if you exposed your data or a part of them to make the problem reproducible. But, here taking a generic case.

library(dplyr)

tibble(uf=c("AM", "RJ", "SC"),
       regiao=c("norte", "sudeste", "sul"),
       ano=c(1989, 1987, 1986),
       pop=runif(3)) %>% 
       print() %>% 
       {.} -> dt

#     uf  regiao   ano        pop
#  <chr>   <chr> <dbl>      <dbl>
#1    AM   norte  1989 0.05092618
#2    RJ sudeste  1987 0.54035176
#3    SC     sul  1986 0.54615493

To change the type of variables, just apply the mutate function of the dplyr package. In the date part you should set a day and a month to use the date type, in the example below I used the first of January, which will be applied to all the lines.

dt %>% 
        mutate(regiao=as.factor(regiao)) %>% 
        mutate(ano=as.Date(ISOdate(ano, 1, 1)))

#     uf  regiao        ano        pop
#  <chr>  <fctr>     <date>      <dbl>
#1    AM   norte 1989-01-01 0.05092618
#2    RJ sudeste 1987-01-01 0.54035176
#3    SC     sul 1986-01-01 0.54615493

What you could do differently here would be to use a single mutate to change both columns, such as:

dt %>% 
        mutate(regiao=as.factor(regiao),
               ano=as.Date(ISOdate(ano, 1, 1)))
    
27.06.2017 / 23:09