'Data in chronological order with condition (on each variable) and looping in a data frame based on condition (per variable)

I would like to organize the rainfall time series data into each rain gauge (by code column) chronologically. The rain gauges are in the same column, specified in lines as in the example, and there are columns with the month, year and rainfall informations.

After organizing the data, I need to perform statistical tests and a loop can make it easier, due to the large number of rain gauges. Is there a way to loop specifying each rain gauge by code in the rain_gauge column, as the variable to be repeated in the tests?

rain_gauge = c(rep(1442032, 40), rep(1442035, 30), rep(1442036, 30),rep(1442039, 45),rep(1442049, 40),rep(1442032, 40),rep(1442045, 35))
year = runif(260, 1978,2020)
month = runif(260,1,12)
rainfall = runif(260, 50,202)

df = data.frame(rain_gauge, year, month, rainfall)#data frame to be organized in chronological order by "code" category
head(df)

#Examples of tests to apply to the series of each rain gauge in the rain_gauge column.
library(modifiedmk)
mmkh(as.vector(subset(df,rain_gauge=="1442032"))$rainfall)

pvalue_mk = mmkh(as.vector(subset(df,rain_gauge=="1442032"))$rainfall)[[2]]#Result to be save in a data frame results

library(tseries)
adf.test(subset(df,rain_gauge=="1442032")$rainfall)
pvalue_df = adf.test(subset(df,rain_gauge=="1442032")$rainfall)[[2]]#Result to be save in a data frame results

Many thanks!



Solution 1:[1]

Consider by, object-oriented wrapper to tapply, that allows you to slice a data frame by factor(s) and run processes on the subsets to return a simplified object (i.e., vector, matrix), or a list of any output:

library(modifiedmk)
library(tseries)

get_pvalues <- function(sub) {
    mmkh_obj <- mmkh(sub$rainfall)
    adf_obj <- adf.test(sub$rainfall)    

    # NAMED VECTOR
    c(pvalue_mmkh = mmkh_obj[, "p.value"], pvalue_adf = adf_obj[, "p.value"])
}

# NAMED MATRIX
pvalues_matrix <- by(df, df$rain_gauge, get_pvalues)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Parfait