r - 如何保存大型数据框并在 R 中快速加载它?

标签 r dataframe wikipedia

<分区>

我目前正在开展一个项目,以提取有关 Wikipedia FR 中 Acadie 门户的定性和定量(统计)数据。有 1905 个条目和 16 个变量。

每次我使用以下代码加载所有统计数据时,加载都需要一些时间。 有没有办法将此 data.frame 保存在我的计算机上并再次加载它以备将来快速使用,同时保持其井井有条?

# Basic information ----

library("WikipediR")

# Function
# How to make function outside of apply: https://ademos.people.uic.edu/Chapter4.html#:~:targetText=vapply%20is%20similar%20to%20sapply,VALUE).&targetText=VALUE%20is%20where%20you%20specify,single%20numeric%20value%2C%20so%20FUN.
pageInfo_fun <- function(portalAcadie_titles){
  page_info(language = "fr", 
            project = "wikipedia", 
            page = portalAcadie_titles,
            properties = c("url"),
            clean_response = T, Sys.sleep(0.0001))} # Syssleep to prevent quote violation.

pageInfo_data <- apply(portalAcadie_titles,1, pageInfo_fun)

# Transform into dataframe

library("tidyverse")
pageInfo_df <- data.frame(map_dfr(pageInfo_data, ~flatten(.)))

它给了我一个可行的数据框,如下所示: enter image description here

当我尝试将它保存到 csv,然后使用 ff 包和 read.csv.ffdf() 时,它没有给我一个可用的数据框。它将所有变量和观察结果合并为一个观察结果,其中包含 20 000 个左右的变量。

最佳答案

您可以轻松地序列化它:

readr::write_rds(pageInfo_df, "pageInfo_df.Rds")

然后像这样反序列化它:

readr::read_rds("pageInfo_df.Rds")

这应该处理任意复杂度的每个有效 R 对象。

关于r - 如何保存大型数据框并在 R 中快速加载它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256555/

相关文章:

mediawiki - 如何在维基百科文章中显示维基数据附加链接

维基百科 API : how to get the number of revisions of a page?

r - Sweave,R,Beamer : How to convert the LaTex text in an Rnw file to R comments?

r - 在 R 的数据框中查找相似的行(不重复)

python - 如果 pandas 数据框中的条件

R - 组合 R 中数据帧内的重复行 :

R ggplot2 : bar chart of a time series

r - 使用 R 提取推文日期

python - 在 Pandas 数据框中插入缺失的类别和日期

python - 使用 BeautifulSoup 和列表从维基百科的信息框中提取特定文本的最佳方法是什么?