[英]Loop for aggregated data frame in R
I have a data frame with 58 columns labeled SD1 through to SD58 along with columns for date info (Date, Year, Month, Day).我有一个数据框,其中包含 58 列标记为 SD1 到 SD58 以及日期信息列(日期、年、月、日)。
I'm trying to find the date of the maximum value of each of the SD columns each year using the following code:我正在尝试使用以下代码查找每年每个 SD 列的最大值的日期:
maxs<-aggregate(SD1~Year, data=SDtime, max)
SDMax<-merge(maxs,SDtime)
I only need the dates so I made a new df and relabeled the column as below:我只需要日期,所以我创建了一个新的 df 并重新标记了该列,如下所示:
SD1Max = subset(SDMax, select = c(Year, Date))
SD1Max %>%
rename(
SD1=Date
)
I want to do the same thing for every SD column but I don't want to have to repeat these steps 58 times.我想对每个 SD 列做同样的事情,但我不想重复这些步骤 58 次。 Is there a way to loop the process?
有没有办法循环这个过程?
Assuming there are no ties (multiple days with where the variable reached its maximum) this probably does what you want:假设没有关系(变量达到最大值的多天),这可能会满足您的要求:
library('tidyverse')
SDtime %>%
pivot_longer(
cols = matches('^SD[0-9]{1,2}$')
) %>%
group_by(name) %>%
filter(value == max(value, na.rm = TRUE)) %>%
ungroup()
You might want to pivot_wider
afterwards.之后你可能想要
pivot_wider
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.