[英]How in R to extract variable, matching on two conditions, within a loop?
[英]R: how to loop read csv files and extract information within each file by matching rows in a reference datatable in R
我有一张参考股票代码表(20,000 行)
和一个包含csv个文件的文件夹,每个CSV个文件以股票代码命名,例如ZTS.csv。 在每个 csv 文件中,是交易品种的价格历史。
最终目标是跟踪所有股票的表现和可视化。 由于参考表和 csv 个文件的庞大规模,我认为最明智的方法是从每个 CSV 个文件中选择需要的信息并将它们添加到参考表中。
例如,我想从参考表中取出一行,符号 ZTS,showdate 2017-01-09,
然后读取ZTE.csv文件,找到日期匹配showdates的行,添加open/high/low/close price数据列
然后循环这个。
由于大小限制,我已将示例数据上传到谷歌驱动器: https://drive.google.com/drive/folders/1G3os67b2i2VfGHnvR6NX8qk1ECuVawGJ?usp=sharing
#read in the reference data
df <- read.csv("reference table.csv", header = TRUE)
# get csv files directory and list all files in this directory
wd <- "/Users/m/Desktop/project/price_data_csv"
files_in_wd <- list.files(wd)
#find stuff to match
# create an empty list and read in all files from wd
mylist <- list()
for(i in seq_along(files_in_wd)){
mylist[[i]] <- read.delim(file = files_in_wd[i],
sep = ',',
header = T)
}
我坚持如何进行匹配和创建组合表。 谢谢
我建议使用data.table
,因为正如@r2evans 提到的那样,它可以很好地分组,而且如果您的数据很大,它会非常快。
使用您的示例数据,可能的 bekow 应该可以帮助您入门(我已经在data.table
方法前面加上前缀以帮助指示它的使用位置)。 您可以在单个符号上使用提供的 function,或者尝试一次运行它(不确定您的数据实际有多大)。
library(data.table)
data_dir <- "~/Downloads/Testing/"
reference_table <- data.table::fread(paste0(data_dir, "reference table.csv"));
prepare_symbol_table <- function(sym, ref) {
# This check is only necessary if calling individually
if(data.table::uniqueN(ref$symbol) > 1)
ref <- ref[symbol == sym]
symbol_csv <- data.table::fread(paste0(data_dir, sym, ".csv"))
data.table::merge.data.table(ref, symbol_csv, by.x = c("showdate"), by.y = c("date"))
}
# merge a single symbol table
yum_table <- prepare_symbol_table("YUM", reference_table)
# all merged at once, reading individual CSVs by matching the symbol column from
# the reference table
all_symbols_merged <- reference_table[, {
# symbol_csv <- data.table::fread(paste0(data_dir, symbol, ".csv"))
# data.table::merge.data.table(.SD, symbol_csv, by.x = c("showdate"), by.y = c("date"))
prepare_symbol_table(.BY, .SD)
}, by = c("symbol")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.