Search code examples
rdata-manipulationreadr

Convert column types to their read_csv() column type in R


One of my favorite things about library(readr) and the read_csv() function in R is that it almost always sets the column types of my data to the correct class. However, I am currently working with an API in R that returns data to me as a dataframe of all character classes, even if the data is clearly numbers. Take this dataframe for example, which has some sports data:

dput(mydf)
structure(list(isUnplayed = c("false", "false", "false"), isInProgress = 
c("false", "false", "false"), isCompleted = c("true", "true", "true"), awayScore = c("106", 
"95", "95"), homeScore = c("94", "97", "111"), game.ID = c("31176", 
"31177", "31178"), game.date = c("2015-10-27", "2015-10-27", 
"2015-10-27"), game.time = c("8:00PM", "8:00PM", "10:30PM"), 
    game.location = c("Philips Arena", "United Center", "Oracle Arena"
    ), game.awayTeam.ID = c("88", "86", "110"), game.awayTeam.City = c("Detroit", 
    "Cleveland", "New Orleans"), game.awayTeam.Name = c("Pistons", 
    "Cavaliers", "Pelicans"), game.awayTeam.Abbreviation = c("DET", 
    "CLE", "NOP"), game.homeTeam.ID = c("91", "89", "101"), game.homeTeam.City = c("Atlanta", 
    "Chicago", "Golden State"), game.homeTeam.Name = c("Hawks", 
    "Bulls", "Warriors"), game.homeTeam.Abbreviation = c("ATL", 
    "CHI", "GSW"), quarterSummary.quarter = list(structure(list(
        `@number` = c("1", "2", "3", "4"), awayScore = c("25", 
        "23", "34", "24"), homeScore = c("25", "18", "23", "28"
        )), .Names = c("@number", "awayScore", "homeScore"), class = "data.frame", row.names = c(NA, 
    4L)), structure(list(`@number` = c("1", "2", "3", "4"), awayScore = c("17", 
    "23", "28", "27"), homeScore = c("26", "20", "25", "26")), .Names = c("@number", 
    "awayScore", "homeScore"), class = "data.frame", row.names = c(NA, 
    4L)), structure(list(`@number` = c("1", "2", "3", "4"), awayScore = c("35", 
    "14", "26", "20"), homeScore = c("39", "20", "35", "17")), .Names = c("@number", 
    "awayScore", "homeScore"), class = "data.frame", row.names = c(NA, 
    4L)))), .Names = c("isUnplayed", "isInProgress", "isCompleted", 
"awayScore", "homeScore", "game.ID", "game.date", "game.time", 
"game.location", "game.awayTeam.ID", "game.awayTeam.City", "game.awayTeam.Name", 
"game.awayTeam.Abbreviation", "game.homeTeam.ID", "game.homeTeam.City", 
"game.homeTeam.Name", "game.homeTeam.Abbreviation", "quarterSummary.quarter"
), class = "data.frame", row.names = c(NA, 3L))

It is quite a hassle to deal with this dataframe once it is returned by the API, given the class types. I've come up with a sort of a hack to update the column classes, which is as follows:

write_csv(mydf, 'mydf.csv')
mydf <- read_csv('mydf.csv')

By writing to CSV and then re-reading the CSV using read_csv(), the dataframe columns update. Unfortunately I am left with a CSV file in my directory that I don't want. Is there a way to update the columns of an R dataframe to their 'read_csv()' column classes, without actually having to write the CSV?

Any help is appreciated!


Solution

  • You don't need to write and read the data if you just want readr to guess you column type. You could use readr::type_convert for that:

    iris %>% 
      dplyr::mutate(Sepal.Width = as.character(Sepal.Width)) %>% 
      readr::type_convert() %>% 
      str()
    

    For comparison:

    iris %>% 
      dplyr::mutate(Sepal.Width = as.character(Sepal.Width)) %>% 
      str()