Reputation: 1565
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 (e.g., dplyr
).
Upvotes: 1
Views: 322
Reputation: 226192
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
Upvotes: 3