简体   繁体   English

在R中逐行读取.CSV

[英]Reading a .CSV row-wise in R

I have been trying to read a large .CSV file (2GB+) with over 900 variables. 我一直在尝试读取具有900多个变量的大型.CSV文件(超过2GB)。 I tried various options to import the file directly but none of them worked. 我尝试了各种选择直接导入文件,但没有一个起作用。

Right now I am trying to import the .CSV file by reading it row-wise. 现在,我试图通过逐行读取来导入.CSV文件。 Which means I use the skip option and append one row at a time to the master file.I am using the following code:- 这意味着我使用了skip选项并一次向主文件追加一行。我正在使用以下代码:-

data<-read.table(file_name,header=TRUE,nrows=1, skip=2,sep=",")

The issue I am facing is that when I use the skip option it doesn't read the headers, even though I have set the header=TRUE. 我面临的问题是,即使我将header设置为TRUE,当我使用skip选项时,它也不会读取标题。

Am I missing something? 我想念什么吗? Any help will be really appreciated. 任何帮助将不胜感激。

This should let you split out your large CSVs into chunks with headers. 这应该使您可以将大型CSV拆分为带有标题的块。

#' Split a large CSV file into separate files with \code{chunk_size} records per-file
#' 
#' @param path path to the large CSV file
#' @param template path template for saving out the smaller files. Uses \code{sprintf}.
#' @param chunk_size number of records per file
#' @param locale,na,quoted_na,comment,trim_ws passed on to \code{read_csv}
#' @examples
#' csv_split("largefile.csv", chunk_size=10000)
csv_split <- function(path, template="file%05d.csv", chunk_size=1000, 
                      locale=default_locale(), na=c("", "NA"), quoted_na=TRUE,
                      comment="", trim_ws=TRUE) {

  require(readr)

  path <- path.expand(path)

  csv_spec <- spec_csv(path)

  skip <- 0
  part <- 1

  repeat {

    df <- read_csv(path, col_names=names(csv_spec$cols), col_types=csv_spec,
                   locale=locale, na=na, quoted_na=quoted_na, comment=comment,
                   trim_ws=trim_ws, skip=skip, n_max=chunk_size)

    if (nrow(df) == 0) break

    cat(sprintf("Writing [%s]...\n", sprintf(template, part)))
    write_csv(df, sprintf(template, part))

    part <- part + 1
    skip <- skip + chunk_size

  }

}

Example: 例:

library(readr)

df <- data.frame(name=sample(LETTERS, 1000000, replace=TRUE),
                 age=sample(30:100, 1000000, replace=TRUE))

write_csv(df, "allinone.csv")

csv_split("allinone.csv", chunk_size=50000)
## Writing [file00001.csv]...
## Writing [file00002.csv]...
## Writing [file00003.csv]...
## Writing [file00004.csv]...
## Writing [file00005.csv]...
## Writing [file00006.csv]...
## Writing [file00007.csv]...
## Writing [file00008.csv]...
## Writing [file00009.csv]...
## Writing [file00010.csv]...
## Writing [file00011.csv]...
## Writing [file00012.csv]...
## Writing [file00013.csv]...
## Writing [file00014.csv]...
## Writing [file00015.csv]...
## Writing [file00016.csv]...
## Writing [file00017.csv]...
## Writing [file00018.csv]...
## Writing [file00019.csv]...
## Writing [file00020.csv]...
## Writing [file00021.csv]...

This [can|should] be modified to handle the edge cases of the first file having chunk_size - 1 records and the possibility of there being commented lines. 可以对此进行修改,以处理具有chunk_size -1个记录的第一个文件的chunk_size以及出现注释行的可能性。

If you don't use this for the actual splitting, you at least have example code for getting/using the column headers. 如果您不将其用于实际拆分,则至少要有示例代码来获取/使用列标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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