【发布时间】:2017-07-18 19:26:48
【问题描述】:
我想要的快速总结是这样的:
我在同一个文件夹中有数千个 .csv 文件,其中包含诸如 discount rate 或 discounted cash flow 之类的短语,主要位于第一列,但也随机位于前 10 列。
使用某些函数(可能是 grepl()、subset() 或 filter()),我想提取包含这些短语的行并将它们与名称一起放入一个新的数据框中他们各自来自的文件。
我遇到的问题是,我一直在尝试的每个功能一次只允许查看一到两列。这是我一直在使用的代码:
#Reading in a single .csv file for now:
MyData <- read.csv("c:/____________/.csv", header = TRUE, sep=",")
#Assigning numbers to each column since each file I will be plugging in has different column headings:
colnames(MyData) <- c(1:ncol(MyData))
#Using subset to check the 1st column and 5th column for discount rate
#(only because I knew these 2 columns contained the phrase "discount rate" ahead of time.)
my.data.frame <- subset(MyData, MyData$`1`=="discount rate" | MyData$`5`=="discount rate")
所以重申一下,我想知道是否有办法搜索许多短语,例如 discount rate、discounted rates 和 discounted cash flow 在某些 data.frame 中的每一列。感谢您提供的任何帮助。
此外,我提供的代码确实会返回包含指定列的行 折扣率,但不包含包含其他词的行,例如折扣率是 5.0%。如果知道此问题的解决方案,我将不胜感激。
【问题讨论】:
-
查看
grep函数。 -
使用 grep,这似乎很复杂,因为您必须指定一个列名来搜索,但我正在查看的所有文件都没有一致的名称或列数