【发布时间】:2015-02-01 13:15:49
【问题描述】:
这是我在 StackOverflow 上的第一个问题。我将尽力使其简洁明了,如果不是,我深表歉意。我也是 R 的新手。我在 StackOverflow 上四处寻找我的问题的答案。我发现了一些可能会有所帮助的点点滴滴,但目前我不确定哪种方法最适合使用,或者如何将它们组合在一起以使其全部发挥作用。
我有一个这样的数据集,叫做“per1”
Day Stat1 Stat2 Stat3
10 2.12 1.84 2.11
10 2.09 1.87 2.07
10 2.08 1.92 2.07
11 1.90 1.85 1.88
11 1.87 1.85 1.93
11 1.86 1.87 1.93
我想要做的是在每一天的每个“统计”列中找到数据的最大值。换句话说,将在每列中计算最大值的行是在 Day 列中包含相同值的行。输出如下所示:
Day MaxStat1 MaxStat2 MaxStat3
10 2.12 1.92 2.11
11 1.87 1.87 1.93
我想创建一个循环来定义 Day 列中唯一值的数量,然后使用它来定义将在每列中计算最大值的行。但是我被困在如何根据独特的日子让 max 函数对每列中的行进行子集化。到目前为止我所拥有的是粗略的,我什至不确定它是否遵循正确的 R 规则(再次,R 的新手)
days <- unique(per1$Day)
stations <- per1[,1:3]
l <- length(days)
for (k in 1:l) {
curr_day <- subset(per1, per1$Day == days[k]) ##this defines the individual day
curr_stn <- stations[curr_day,] ##this is supposed to define the number of rows as the number of rows in curr_day
for(i in 1:stations) { ##loop over each column
max[i] <- max(stations[curr_day,curr_stn]) ##take the maximum for each column based on the number of rows for each curr_day
}
}
我得到了
Error in stations[curr_day, ] : subscript out of bounds
所以我认为这意味着我没有正确定义我的论点。如果有人可以帮助我为这个循环设置正确的格式,那将不胜感激!任何其他更清洁/更快的方法也将受到欢迎。 (我查看了“mapply”,但不知道如何编写将 Stat 列的行数定义为每个唯一天的行数的函数)
感谢您的宝贵时间。
【问题讨论】:
标签: r loops for-loop max subset