【问题标题】:Get the number of rows and columns of a multiple CSV file获取多个 CSV 文件的行数和列数
【发布时间】:2022-01-06 02:32:49
【问题描述】:

有没有办法在 R 中获取有关多个 CSV 文件的行数和列数的信息并将其保存在 CSV 文件中?这是我的 R 代码:

#Library
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("fs")) install.packages("fs")

#Mentioning Files Location
file_paths <- fs::dir_ls("C:\\Users\\Desktop\\FileCount\\Test")
file_paths[[2]]


#Reading Multiple CSV Files
file_paths %>%
  map(function(path)
  {
     read_csv(path,col_names = FALSE)
  })

#Counting Number of Rows
lapply(X = file_paths, FUN = function(x) {
  length(count.fields(x))
})

#Counting Number of Columns
lapply(X = file_paths, FUN = function(x) {
  length(ncol(x))
})

#Saving CSV File
write.csv(file_paths,"C:\\Users\\Desktop\\FileCount\\Test\\FileName.csv", row.names = FALSE)

有几件事不起作用:

  1. 多个 CSV 文件的列数
  2. 当我保存文件时,我想保存文件名、行数和列数。见附图。

输出的样子:

附上一些用于测试的CSV文件:Here

任何帮助表示赞赏。

【问题讨论】:

    标签: r csv


    【解决方案1】:

    欢迎!使用tidyversedata.table,这是一种方法:

    注意:所有 .csv 文件都在我的 TestStack 目录中,但您可以使用自己的目录 (C:/Users/Desktop/FileCount/Test) 进行更改。

    代码:

    library(tidyverse)
    csv.file <- list.files("TestStack") # Directory with your .csv files
    data.frame.output <- data.frame(number_of_cols = NA,
                                    number_of_rows = NA,
                                    name_of_csv = NA) #The df to be written
    
    
    MyF <- function(x){
      
      csv.read.file <- data.table::fread(
        paste("TestStack", x, sep = "/")
        )
      
      number.of.cols <- ncol(csv.read.file)
      
      number.of.rows <- nrow(csv.read.file)
      
      data.frame.output <<- add_row(data.frame.output,
                                    number_of_cols = number.of.cols,
                                    number_of_rows = number.of.rows,
                                    name_of_csv = str_remove_all(x,".csv")) %>% 
        filter(!is.na(name_of_csv))
      
    }
    
    map(csv.file, MyF)
    

    输出:

      number_of_cols number_of_rows name_of_csv
    1              3           2150      CH_com
    2              2          34968 epci_com_20
    3              3            732        g1g4
    4              7         161905          RP
    

    我有这个输出是因为我的 TestStack 有 4 个文件,分别命名为 CH_com.csv、epci_com_20.csv、...

    然后,您可以根据需要将对象 data.frame.output 写入 .csv:data.table::fwrite(data.frame.output, file = "Output.csv")

    【讨论】:

    • 我建议读取文件一次,并抓住nrow和ncol,没有理由读取文件两次。
    • @MerijnvanTilborg 是的,你是对的,我在看到预期的行数后添加了第二个阅读,我会更新
    【解决方案2】:
    files_map <- "test"
    files <- list.files(files_map)
    
    library(data.table)
    
    output <- data.table::rbindlist(
      lapply(files, function(file) {
        dt <- data.table::fread(paste(files_map, file, sep = "/"))
        list("number_of_cols" = ncol(dt), "number_of_rows" = nrow(dt), "name_of_csv" = file)
      })
    )
    
    data.table::fwrite(output, file = "Filename.csv")
    

    或者使用 map 和单独的函数来完成任务,但不首先使用空表并使用全局分配对其进行更新。我看到这在应用函数上经常发生,而根本不需要它。

    myF <- function(file) {
        dt <- data.table::fread(paste(files_map, file, sep = "/"))
        data.frame("number_of_cols" = ncol(dt), "number_of_rows" = nrow(dt), "name_of_csv" = file)
    }
    
    output <- do.call(rbind, map(files, myF))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多