[英]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? 是否可以指定
read_csv
从readr
包装应返回data.frame而不是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. 我的问题是我的一些来自pre-tibble时间的代码不再运行,因为列的命名是关闭的。 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): 我可以使用
as.data.frame
将数据块转换为数据帧,但与我首先创建数据框(输出已注释掉)相比,列的命名仍然不同:
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. 我可以(以及将来的项目)使用
checkpoint
包来使用我最初运行这些项目时的软件包版本。 However, for now I am looking for an option where I can specify whether I get a tibble or a data frame. 但是,现在我正在寻找一个选项,我可以指定是获得tibble还是数据帧。
This question applies to other packages from the tidyverse as well (eg, dplyr
). 此问题也适用于来自tidyverse的其他包(例如,
dplyr
)。
You can use make.names()
to hack around this inconsistency: 您可以使用
make.names()
来解决这种不一致问题:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.