I'm trying to run a function from the modifiedmk package in R.
install.packages('modifiedmk')
library(modifiedmk)
I have a dataframe data which I produced with the following:
Station <- c('APT','APT', 'APT','APT', 'APT', 'APT', 'APT','APT', 'APT','APT','APT','APT',
'AF','AF', 'AF','AF','AF','AF','AF','AF','AF',
'EL', 'EL', 'EL', 'EL', 'EL', 'EL', 'EL', 'EL', 'EL', 'EL', 'EL', 'EL', 'EL', 'EL', 'EL',
'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS', 'GFS'
)
Rainfall <- c(375.3, 263.3, 399.2, 242.6, 847.6, 276.5, 712.8, 366.3, 188.6, 478.4, 539, 682.5,
520.7, 1337.8, 524, 908.4,748.5,411.8, 772.4,978.5,983,
732.4, 788.6, 567.1, 576, 931.6, 727.2, 1079.3, 902.8,493.4, 630.7, 784.1,660.2, 531.3, 487.1,798.4,
1064.1, 590.3, 1011.2, 1037.1, 1398.4, 1153.6,994.1, 1100.2,743.7,637.4, 792.2, 891.9,880.9, 670, 920.2,681.4)
Year <- c('1957','1958','1959','1960','1961','1962','1963','1964','1965','1966','1967','1968',
'1960','1961','1962','1963','1964','1965','1966','1967','1968',
'1957','1958','1959','1960','1961','1962','1963','1964','1965','1966','1967','1968','1969','1970','1971',
'1964','1965','1966','1967','1968','1969','1970','1971','1972','1973','1974','1975','1976','1977','1978','1979')
length(Year)
data<-data.frame(Year, Station, Rainfall)
where I have four Stations of rainfall data as rows in the dataframe. I want to apply the mmky1lag method from the modifiedmk package on each Station of data and produce a summary table in R that has two columns:
- The percent of stations with significant trends where p < 0.05
- The average Sen's slope
For example, I can run the mmky1lag method on all of the Rainfall data using mmky1lag(as.vector(data$Rainfall)) which produces
> mmky1lag(as.vector(data$Rainfall))
Corrected Zc new P-value N/N* Original Z old P.value
3.332353e+00 8.611480e-04 1.297360e+00 3.795608e+00 1.472822e-04
Tau Sen's slope old.variance new.variance
3.634992e-01 9.092857e+00 1.605933e+04 2.083474e+04
And I'm interested in two of those outputs:
Column 1:
# Get percent of stations with significant trends where p < 0.05
mmky1lag(as.vector(data$Rainfall))[2] < 0.05
and Column 2:
# Make another column that is the mean Sen's slope
mmky1lag(as.vector(data$Rainfall))[7]
However, how do I apply this method on data where I get a result for each individual Station? In python, I would groupby Station and then apply the method. But I'm not sure how to do that in R.
Then after grouping by station, I want a summary table with the two aforementioned columns of information.