I am trying to run a MannKendall over a group of files, to perform the MK on each one. This is precip data, for each area, over decades. I read the data into R, then create df "datalist". I bind all the files into one, and then split them by the name in the first column ("V1"). This all works fine. Then I want to perform the MK on each split "chunk". Previously, I have used this same code to create means of each "chunk" and it worked perfectly:
I was hoping to just slightly tweak the code like this:
, where x is the timeline and y is the data.
But I am getting "Error in Kendall(x$V2, y$V3) : object 'y' not found". I am confused about the requirements for the MK test.
I have the manual and it states, "Usage Kendall(x, y) Arguments x first variable, a vector y second variable, a vector the same length as x Details In many applications x and y may be ranks or even ordered categorical variables. In our function x and y should be numeric vectors or factors. Any observations corresponding to NA in either x or y are removed. Kendall’s rank correlation measures the strength of monotonic association between the vectors x and y."
This is not clear to me. My data are a vector of precip data over a time period. I am not comparing it to something else, I only want to see if there is a trend in the data. Could someone please explain what I am missing? Should I be using a different test from the package? I find the manual very cryptic and confusing. I tried to run MannKendall(x$V3) and got results, but I don't understand what is being correlated? There are no "pairs", it is a string of data records. So, I am not understanding how to interpret the results.
Attached please find a small sample of the data. All columns are the same length. There are no blank rows or "NA" cells.
This is my full code:
Attached find 3 sample files. NOTE: The timelines are abbreviated for these samples, hence the code above "skip = 56" should be ignored. Also, the column headers "HUC8", "YEAR", and "RO_MM" are lost in the process, due to the fact that I am skipping rows. The "V1", "V2", "V3" are created by the script, so I just ran with them.
Thanks for your attention.
Code:
Samp < sapply(splitByHUCs, function(x) mean(x$RO_MM, na.rm = FALSE, use.names = T))
Code:
Samp < sapply(splitByHUCs, function(x) Kendall(x$V2, y$V3))
But I am getting "Error in Kendall(x$V2, y$V3) : object 'y' not found". I am confused about the requirements for the MK test.
I have the manual and it states, "Usage Kendall(x, y) Arguments x first variable, a vector y second variable, a vector the same length as x Details In many applications x and y may be ranks or even ordered categorical variables. In our function x and y should be numeric vectors or factors. Any observations corresponding to NA in either x or y are removed. Kendall’s rank correlation measures the strength of monotonic association between the vectors x and y."
This is not clear to me. My data are a vector of precip data over a time period. I am not comparing it to something else, I only want to see if there is a trend in the data. Could someone please explain what I am missing? Should I be using a different test from the package? I find the manual very cryptic and confusing. I tried to run MannKendall(x$V3) and got results, but I don't understand what is being correlated? There are no "pairs", it is a string of data records. So, I am not understanding how to interpret the results.
Attached please find a small sample of the data. All columns are the same length. There are no blank rows or "NA" cells.
dput (splitByHUCs)
shows this: "row.names = c(NA, 84L), class = c("data.table", "data.frame")"This is my full code:
Code:
fnames < dir("~/Desktop/test_files/", pattern = "*_45Fall_*")
read_data < function(z){
dat < fread(z, skip = 56, select = 1:3)
}
datalist < lapply(fnames, read_data)
bigdata < rbindlist(datalist, use.names = T)
splitByHUCs < split(bigdata, f = bigdata$V1, sep = "\n", lex.order = TRUE)
# So far, so good.
Samp < sapply(splitByHUCs, function(x) Kendall(x$V2, y$V3))
write.csv(Samp, file = "~/Desktop/results/Fall.csv", row.names = FALSE)
Thanks for your attention.
Attachments

20 KB Views: 0
Last edited: