I want to apply mk.test() to the large dataset and get results in a table/matrix.
My data look something like this:
Column A | Column B | ... | ColumnXn |
---|---|---|---|
1 | 2 | ... | 5 |
... | ... | ... | ... |
3 | 4 | ... | 7 |
So far I managed to perform mk.test() for all columns and print the results:
for(i in 1:ncol(data)) {
print(mk.test(as.numeric(unlist(data[ , i]))))
}
I got all the results printed:
.....
Mann-Kendall trend test
data: as.numeric(unlist(data[, i])) z = 4.002, n = 71, p-value = 6.28e-05 alternative hypothesis: true S is not equal to 0 sample estimates: S varS tau 7.640000e+02 3.634867e+04 3.503154e-01
Mann-Kendall trend test
data: as.numeric(unlist(data[, i])) z = 3.7884, n = 71, p-value = 0.0001516 alternative hypothesis: true S is not equal to 0 sample estimates: S varS tau 7.240000e+02 3.642200e+04 3.283908e-01
....
However, I was wondering if it is possible to get results in a table/matrix format that I could save as excel. Something like this:
Column | z | p-value | S | varS | tau |
---|---|---|---|---|---|
Column A | 4.002 | 0.0001516 | 7.640000e+02 | 3.642200e+04 | 3.283908e-01 |
... | ... | ... | ... | ... | ... |
ColumnXn | 3.7884 | 6.28e-05 | 7.240000e+02 | 3.642200e+04 | 3.283908e-01 |
Is it possible to do so? I would really appreciate your help.
Instead of printing the test results you can store them in a variable. This variable holds the various test statistics and values. To find the names of the properties you can perform the test on the first row and find the property names using a string conversion:
Here you see that for example the z-value is called
testres$statistic
and similar for the other properties. The values of S, varS and tau are not separate properties but they are grouped together in the listtestres$estimates
.In the code you can create an empty dataframe, and in the loop add the results of that run to this dataframe. Then at the end you can convert to csv using
write.csv()
.The resulting csv file can be opened in Excel.