1

I have a tibble (data.frame) that I need to apply a number of type updates to. I have a readr::col_spec object that describes the desired types, but since the data does not originate as a csv file, I cannot use read_csv(..., col_types=cspec) to apply the changes to the specified columns.

Since col_spec is a data structure designed exactly to specify desired data types, I would nevertheless to use it directly as an input to a function that applies the changes for me, rather than writing a long custom script to apply the different columns. See the following example:

library(tidyverse)

# Subset starwars to get sw (comparable to my input data)
sw <- starwars %>%
  select(name, height, ends_with("_color")) %>%
  slice(c(1,4,5,19))
sw
#> # A tibble: 4 × 5
#>   name           height hair_color skin_color eye_color
#>   <chr>           <int> <chr>      <chr>      <chr>    
#> 1 Luke Skywalker    172 blond      fair       blue     
#> 2 Darth Vader       202 none       white      yellow   
#> 3 Leia Organa       150 brown      light      brown    
#> 4 Yoda               66 white      green      brown

# The col_spec that I have
cspec <- cols(
  hair_color = col_factor(c("brown", "blond", "white", "none")),
  skin_color = col_factor(c( "green", "light", "fair", "white")),
  eye_color = col_factor(c("blue", "brown", "yellow"))
)

# I would like to apply the col_spec directly to sw

# A not so great workaround is to use a tempfile
tf <- tempfile()
sw %>% write_csv(tf)
sw_fct <- read_csv(tf, col_types=cspec)

# This is more or less the result I am after:
# But note how info on other columns (height) is lost in the roundtrip
sw_fct
#> # A tibble: 4 × 5
#>   name           height hair_color skin_color eye_color
#>   <chr>           <dbl> <fct>      <fct>      <fct>    
#> 1 Luke Skywalker    172 blond      fair       blue     
#> 2 Darth Vader       202 none       white      yellow   
#> 3 Leia Organa       150 brown      light      brown    
#> 4 Yoda               66 white      green      brown
Magnus
  • 23,900
  • 1
  • 30
  • 28

3 Answers3

1

We may do this by extracting the elements from the object by looping overs the cols

library(readr)
library(purrr)
sw[names(cspec$cols)] <- imap(cspec$cols, ~ parse_factor(sw[[.y]],
     levels = .x$levels, ordered = .x$ordered, include_na = .x$include_na))

-checking the output

> sw
# A tibble: 4 × 5
  name           height hair_color skin_color eye_color
  <chr>           <int> <fct>      <fct>      <fct>    
1 Luke Skywalker    172 blond      fair       blue     
2 Darth Vader       202 none       white      yellow   
3 Leia Organa       150 brown      light      brown    
4 Yoda               66 white      green      brown    

> str(sw)
tibble [4 × 5] (S3: tbl_df/tbl/data.frame)
 $ name      : chr [1:4] "Luke Skywalker" "Darth Vader" "Leia Organa" "Yoda"
 $ height    : int [1:4] 172 202 150 66
 $ hair_color: Factor w/ 4 levels "brown","blond",..: 2 4 1 3
 $ skin_color: Factor w/ 4 levels "green","light",..: 3 4 2 1
 $ eye_color : Factor w/ 3 levels "blue","brown",..: 1 3 2 2

If we also need the attributes of 'spec', do the assignment

attr(sw, "spec") <- cspec

-checking the str

> str(sw)
tibble [4 × 5] (S3: tbl_df/tbl/data.frame)
 $ name      : chr [1:4] "Luke Skywalker" "Darth Vader" "Leia Organa" "Yoda"
 $ height    : int [1:4] 172 202 150 66
 $ hair_color: Factor w/ 4 levels "brown","blond",..: 2 4 1 3
 $ skin_color: Factor w/ 4 levels "green","light",..: 3 4 2 1
 $ eye_color : Factor w/ 3 levels "blue","brown",..: 1 3 2 2
 - attr(*, "spec")=
  .. cols(
  ..   hair_color = col_factor(levels = c("brown", "blond", "white", "none"), ordered = FALSE, include_na = FALSE),
  ..   skin_color = col_factor(levels = c("green", "light", "fair", "white"), ordered = FALSE, include_na = FALSE),
  ..   eye_color = col_factor(levels = c("blue", "brown", "yellow"), ordered = FALSE, include_na = FALSE)
  .. )
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Great answer, I provided an alternative one wrapping the oneliner into a function, but accepted this as the official answer. – Magnus Oct 14 '21 at 21:07
1

This answer wraps the solution from @akrun into a function for those who may be less versed in purrr.

apply_col_spec <- function(d, cspec, set_spec_attribute=FALSE) {
  
  # A bit of input checking
  if (!all(inherits(d, "data.frame"), inherits(cspec, "col_spec"), 
           is.logical(set_spec_attribute))) {
    stop("apply_col_spec(): wrong input types")
  }
  if (!all(sapply(cspec$cols, inherits, "collector_factor"))) {
    stop("apply_col_spec(): only implemented for factor columns")
  }
  
  # Do the actual application of the col_spec
  d[names(cspec$cols)] <- imap(cspec$cols, ~ parse_factor(d[[.y]],
     levels = .x$levels, ordered = .x$ordered, include_na = .x$include_na))
  
  # If requested, set col_spec as an attribute, for consistency with readr
  if (set_spec_attribute) {
    attr(d, "spec") <- cspec
  }
  d
}

And running the function on the variables defined in the question yields the expected result:

> apply_col_spec(sw, cspec)
# A tibble: 4 × 5
  name           height hair_color skin_color eye_color
  <chr>           <int> <fct>      <fct>      <fct>    
1 Luke Skywalker    172 blond      fair       blue     
2 Darth Vader       202 none       white      yellow   
3 Leia Organa       150 brown      light      brown    
4 Yoda               66 white      green      brown    
Magnus
  • 23,900
  • 1
  • 30
  • 28
0

An alternative way of achieving this, which seems better in retrospect, is to use the readr::type_convert() function. This function has almost exactly the same behavior as the apply_col_spec() function below, and comes shrink-wrapped with the readr package.

Magnus
  • 23,900
  • 1
  • 30
  • 28