简体   繁体   中英

readr (or other packages from tidyverse) with data.frame instead of tibble

Is it possible to specify that read_csv from the readr package should return a data.frame instead of a tibble? I might have overlooked it but I could not find such an option in the package manual.

My problem is that some of my code from pre-tibble times does not run anymore because the naming of the columns is off. I can convert tibbles to data frames with as.data.frame , but the naming of columns will still be different compared to when I had created a data frame in the first place (output commented out):

df <- data.frame("((hello))" = 1)
df
#  X..hello..
# 1          1
tb <- tibble("((hello))" = 1)
tb
## A tibble: 1 × 1
#  `((hello))`
#       <dbl>
#1           1
as.data.frame(tb)
#  ((hello))
#1         1

I could (and will for future projects) use the checkpoint package to use package versions from when I originally ran these projects. However, for now I am looking for an option where I can specify whether I get a tibble or a data frame.

This question applies to other packages from the tidyverse as well (eg, dplyr ).

You can use make.names() to hack around this inconsistency:

dfconv <- function(x) {
     return(setNames(as.data.frame(x),
              make.names(names(x))))
}

library(tibble)
df <- data.frame("((hello))" = 1)
tb <- tibble("((hello))" = 1)
identical(dfconv(tb),df)  ## TRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM