如何从 R 中的多个 csv 文件中提取列
How to extract columns from multiple csv files in R
抱歉这个简单的问题。我有一个非常基本的 R 脚本,它在一个非常大的 *.csv 文件中的特定列下提取和计算相同项目的出现次数,并给我一个项目列表及其频率,如下所示:
COLUMNNAME freq
item1 15
item2 7
item3 500
and so on...
脚本是这样的:
library(plyr)
my_file<-read.csv(file='file1.csv', header=TRUE, sep = '')
count(my_file, vars='COLUMNNAME')
我的问题是如何对我的其他 25 个 *.csv 文件再次执行此操作并将结果写入 output.csv 文件,如下所示:
COLUMNNAME file1.csv file2.csv ......
item1 15 ?
item2 7 ?
item3 500 ?
and so on...
这些文件的结构相同,包含相同的项目,但频率不同。任何帮助深表感谢。提前致谢。
我的 csv 示例文件的一个快照是这样的:
您可以列出文件,例如使用 list.files
。然后你可以遍历文件,加载它们,并将它们添加到新的 data.frame
的底部。类似于:
library(tidyverse)
csv.files <- list.files(pattern="*.csv", recursive=TRUE)
bg.df <- NULL
for (csv.file in csv.files) {
if (is.null(bg.df)) {
bg.df <- readr::read_csv(csv.file) %>%
dplyr::mutate(
file = csv.file
)
} else {
bg.df <- bg.df %>%
dplyr::add_row(
readr::read_csv(csv.file) %>%
dplyr::mutate(
file = csv.file
)
)
}
}
现在,您还可以使用 tidyr::pivot_wider
将它们再次变宽,但我不确定您的数据是什么样子。
无论如何,如果需要,您可以使用 dplyr::group_by
和 dplyr::summarize
进行总结。但是要写那一点,我需要一些例子 data/files.
已编辑:我添加了评论中的代码。
看看这是否有效
library(tidyverse)
library(data.table)
# udf for collecting frequencies
tableIt <- function(tbl){
fread(tbl) %>%
select(colChosen) %>% # change colChosen to the column name you're looking for
{table(.)}
}
# collect file names and call udf
filesToReview <-
list.files(path = whateverYourPathIs,
pattern = "*.csv",
full.names = T) %>% # keep full directory
# if you have files in subfolders, you can add recursive = T to look there, too
set_names(str_extract(., "([^\/]+$)")) %>%
map(~tableIt(.))
抱歉这个简单的问题。我有一个非常基本的 R 脚本,它在一个非常大的 *.csv 文件中的特定列下提取和计算相同项目的出现次数,并给我一个项目列表及其频率,如下所示:
COLUMNNAME freq
item1 15
item2 7
item3 500
and so on...
脚本是这样的:
library(plyr)
my_file<-read.csv(file='file1.csv', header=TRUE, sep = '')
count(my_file, vars='COLUMNNAME')
我的问题是如何对我的其他 25 个 *.csv 文件再次执行此操作并将结果写入 output.csv 文件,如下所示:
COLUMNNAME file1.csv file2.csv ......
item1 15 ?
item2 7 ?
item3 500 ?
and so on...
这些文件的结构相同,包含相同的项目,但频率不同。任何帮助深表感谢。提前致谢。
我的 csv 示例文件的一个快照是这样的:
您可以列出文件,例如使用 list.files
。然后你可以遍历文件,加载它们,并将它们添加到新的 data.frame
的底部。类似于:
library(tidyverse)
csv.files <- list.files(pattern="*.csv", recursive=TRUE)
bg.df <- NULL
for (csv.file in csv.files) {
if (is.null(bg.df)) {
bg.df <- readr::read_csv(csv.file) %>%
dplyr::mutate(
file = csv.file
)
} else {
bg.df <- bg.df %>%
dplyr::add_row(
readr::read_csv(csv.file) %>%
dplyr::mutate(
file = csv.file
)
)
}
}
现在,您还可以使用 tidyr::pivot_wider
将它们再次变宽,但我不确定您的数据是什么样子。
无论如何,如果需要,您可以使用 dplyr::group_by
和 dplyr::summarize
进行总结。但是要写那一点,我需要一些例子 data/files.
已编辑:我添加了评论中的代码。
看看这是否有效
library(tidyverse)
library(data.table)
# udf for collecting frequencies
tableIt <- function(tbl){
fread(tbl) %>%
select(colChosen) %>% # change colChosen to the column name you're looking for
{table(.)}
}
# collect file names and call udf
filesToReview <-
list.files(path = whateverYourPathIs,
pattern = "*.csv",
full.names = T) %>% # keep full directory
# if you have files in subfolders, you can add recursive = T to look there, too
set_names(str_extract(., "([^\/]+$)")) %>%
map(~tableIt(.))