在 R 中的多个文件上应用相同的函数 [英] applying same function on multiple files in R

查看:21
本文介绍了在 R 中的多个文件上应用相同的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 R 程序的新手,目前正在研究一组财务数据.现在我的工作目录下有大约 10 个 csv 文件,我想分析其中一个并将相同的命令应用于其余的 csv 文件.

I am new to R program and currently working on a set of financial data. Now I got around 10 csv files under my working directory and I want to analyze one of them and apply the same command to the rest of csv files.

以下是这些文件的所有名称:(US%10y.csv"、UK%10y.csv"、GER%10y.csv"、JAP%10y.csv"、CHI%10y.csv"、SWI%10y.csv"、SOA%10y.csv"、BRA%10y.csv"、CAN%10y.csv"、AUS%10y.csv")

Here are all the names of these files: ("US%10y.csv", "UK%10y.csv", "GER%10y.csv","JAP%10y.csv", "CHI%10y.csv", "SWI%10y.csv","SOA%10y.csv", "BRA%10y.csv", "CAN%10y.csv", "AUS%10y.csv")

例如,因为CSV文件中的Date列是Factor,所以我需要将它们更改为Date格式:

For example, because the Date column in CSV files are Factor so I need to change them to Date format:

CAN <- read.csv("CAN%10y.csv", header = T, sep = ",")
CAN$Date <- as.character(CAN$Date)
CAN$Date <- as.Date(CAN$Date, format ="%m/%d/%y")
CAN_merge <- merge(all.dates.frame, CAN, all = T)
CAN_merge$Bid.Yield.To.Maturity <- NULL

all.dates.frame 是连续 731 天的数据框.我想合并它们,以便每个文件都具有相同的行数,这使我以后可以将 10 个文件组合在一起以获得 731 X 11 的主数据框.

all.dates.frame is a data frame of 731 consecutive days. I want to merge them so that each file will have the same number of rows which later enables me to combine 10 files together to get a 731 X 11 master data frame.

我当然可以复制和粘贴此代码并更改文件名,但是有没有简单的方法可以使用 apply 或 for 循环来做到这一点???

Surely I can copy and paste this code and change the file name, but is there any simple approach to use apply or for loop to do that ???

非常感谢您的帮助.

推荐答案

这应该可以解决问题.如果某个部分不起作用,请发表评论.不经测试就瞎写了.

This should do the trick. Leave a comment if a certain part doesn't work. Wrote this blind without testing.

获取当前目录中以名称 .csv

Get a list of files in your current directory ending in name .csv

L = list.files(".", ".csv")

遍历每个名​​称并读入每个文件,执行您想要执行的操作,返回 data.frame DF_Merge 并将它们存储在列表中.

Loop through each of the name and reads in each file, perform the actions you want to perform, return the data.frame DF_Merge and store them in a list.

O = lapply(L, function(x) {
           DF <- read.csv(x, header = T, sep = ",")
           DF$Date <- as.character(CAN$Date)
           DF$Date <- as.Date(CAN$Date, format ="%m/%d/%y")
           DF_Merge <- merge(all.dates.frame, CAN, all = T)
           DF_Merge$Bid.Yield.To.Maturity <- NULL
           return(DF_Merge)})

将所有DF_Merge data.frames 绑定到一个 big data.frames

Bind all the DF_Merge data.frames into one big data.frame

do.call(rbind, O)

我猜你需要某种指标,所以这可能很有用.根据文件名的前 3 个字符创建一个指标列 rep(substring(L, 1, 3), each = 731)

I'm guessing you need some kind of indicator, so this may be useful. Create a indicator column based on the first 3 characters of your file name rep(substring(L, 1, 3), each = 731)

这篇关于在 R 中的多个文件上应用相同的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆