简体   繁体   中英

R Write multiple MannKendall results to data frame or csv

I'm a R beginner, and I'm struggling to find a solution to something that's probably extremely straightforward. Help appreciated.

I'm evaluating sulfate trends in >1,000 groundwater wells using the MannKendall package in R, and I've been storing results as individual lists. I'd like to combine all the results into a single dataframe, so I can target wells with increasing concentrations and export results to CSV and share with folks who don't know how to use R.

#Example:
library(Kendall)
w1<-c(4.3,5.7,2.4,9.8,6.7,3.9,8.3,9.6,4.7)
w2<-c(3.2,5.8,9.9,14.6,17.8,13.5,20.4,78.9,50.3)
w1mk<-MannKendall(w1)
w2mk<-MannKendall(w2)

#Next step: combine and store w1mk and w2mk results as data frame for analysis/export

not sure how you would like your data frame to look like, but anyhow check this

as.data.frame(rbind(unlist(w1mk),unlist(w2mk)))

or

library(dplyr)
bind_rows(unlist(w1mk),unlist(w2mk))

@CBernhardt I'd break this down into small manageable steps to see what's going on with your data...

Starting with your example data...

library(Kendall)
w1<-c(4.3,5.7,2.4,9.8,6.7,3.9,8.3,9.6,4.7)
w2<-c(3.2,5.8,9.9,14.6,17.8,13.5,20.4,78.9,50.3)

First thing I would do is to put all these individual lists into one big dataframe with one site (well) per column

groundwater <- data.frame(w1=w1,w2=w2)
groundwater

Then use a simple lapply command to run the test across each column (well/site)

allofthem <- lapply(groundwater, function(y) unlist(MannKendall(y)))

allofthem is now a list of the Mann Kendall results per site...

allofthem

#$w1
#       tau         sl          S          D       varS 
# 0.2222222  0.4655123  8.0000000 36.0000000 92.0000000 

When you're sure that's working through it all in a dataframe

MKResults <- as.data.frame(allofthem)
MKResults

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM