【发布时间】:2018-06-09 09:36:18
【问题描述】:
对于以下问题,我将不胜感激:
我有多个巨大的日志文件(每个 >1.000.000 个条目),其中包含一些我特别感兴趣的行(行)。所以我想制作一个仅包含这些行的子集,但我想将结果写入包含多个日志文件/参与者信息的矩阵中。因此,我创建了一小行代码来 1. 创建子集并 2. 在循环中运行它,不仅针对其中一个日志文件,而且针对所有日志文件。
Result <- subset(df, df$columnOfInterest== "interestingCondition1" | df$columnOfInterest== "interestingCondition2" | df$columnOfInterest== "interestingCondition3")$columnOfInterest
View(Result)
1
interestingCondition1
2
interestingCondition1
3
interestingCondition2
4
interestingCondition1
5
interestingCondition1
6
interestingCondition3
7
interestingCondition2
8
interestingCondition1
9
interestingCondition1
10
interestingCondition1
嵌入到循环中:
WrongResult <- matrix(data=NA,nrow=TrialNumber, ncol=length(ListOfFiles))
vpncount <- 1
for (v in ListOfFiles){
df<- read.delim(v, header = TRUE, sep='\t')
WrongResult[,vpncount] <- subset(df, df$columnOfInterest== "interestingCondition1" | df$columnOfInterest== "interestingCondition2" | df$columnOfInterest== "interestingCondition3")$columnOfInterest
vpncount <- vpncount+1
}
在一个日志文件上运行代码时,我得到了我想要的结果,但是当通过循环运行它时,它会创建一个具有适当大小的矩阵,但只是填充了“随机”数字而不是我细分的条件.
有谁知道为什么会发生这种情况以及如何解决它?非常感谢任何帮助!
编辑:
我尝试创建一个示例数据框。第一行代码(包括变量 Results)就像我想要的那样工作。它在我的 columnOfInterest 的行上过滤我的数据框并将它们放入一个新矩阵中。但是,如果我尝试在一个循环中运行它不止一个数据帧,我就会一直遇到错误:
df <- data.frame(
X = sample(1:10),
columnOfInterest= sample(c("interestingCondition1", "interestingCondition2", "interestingCondition3", "NotinterestingCondition1"), 10, replace = TRUE)
)
View(df)
Result <- subset(df, df$columnOfInterest== "interestingCondition1" | df$columnOfInterest== "interestingCondition2" | df$columnOfInterest== "interestingCondition3")$columnOfInterest
View(Result)
WrongResult <- matrix(data=NA,nrow=280, ncol=20)
vpncount <- 1
for (v in 1:20){
df<- read.delim(v, header = TRUE, sep='\t')
WrongResult[,vpncount] <- subset(df, df$columnOfInterest== "interestingCondition1" | df$columnOfInterest== "interestingCondition2" | df$columnOfInterest== "interestingCondition3")$columnOfInterest
vpncount <- vpncount+1
}
View(WrongResult)
【问题讨论】:
-
我没有看到你的 sapply 函数?
-
是的,你说得对,我真的很抱歉。我测试了几个解决方案,上面我用子集()替换了 sapply 行。这是另一个想法,对我来说也很有效,但是将其放入循环(因此在多个数据帧上运行)的问题仍然存在。
-
WrongResult2
-
请更正样本数据中的错别字 --
columnOfInterest,interestingCondition1 -
@MartinMorgan 完成,对不起!