Reputation: 19
Would you mind to help me to run the data frame generated from subset in loop. I want to do Mann-Kendall test (Library (rkt))for each subset data is similar to bellow:
date Year Month Day sum-rainfall frac overall-slope Sens Slope Seq
1 1911-10-01 1911 10 1 216.3 1911.11 278.5459 a)1911-2019: -0.85 mm/yr 1
2 1912-10-01 1912 10 1 275.3 1912.11 277.7006 a)1911-2019: -0.85 mm/yr 1
3 1913-10-01 1913 10 1 266.2 1913.11 276.8553 a)1911-2019: -0.85 mm/yr 1
.
.
78 1988-10-01 1988 10 1 225.6 1988.11 213.4582 a)1911-2019: -0.85 mm/yr 4
79 1989-10-01 1989 10 1 234.6 1989.11 212.6129 a)1911-2019: -0.85 mm/yr 4
.
.
108 2018-10-01 2018 10 1 206.8 2018.11 188.0994 a)1911-2019: -0.85 mm/yr 6
109 2019-10-01 2019 10 1 168.8 2019.11 187.2541 a)1911-2019: -0.85 mm/yr 6
for (i in rainfall_wet$Seq){
m<- assign(paste("segment_wet", i, sep = "_"),subset(rainfall_wet, Seq==i))
FD<-rkt(m$Year,m$sum_rainfall,m$Month,,TRUE)
print(FD)
FD$B
b0<- median(m$sum_rainfall)-(FD$B* median(m$Year))
m$overall_slope<- b0+(FD$B*m$Year)
length(m$Year)
a3<-as.character(m$Year[1])
b3<-as.character(m$Year[length(m$Year)])
z3<-as.character(lapply(FD$B, round,2))
m$`Sens Slpe`<- paste("b)",a3,"-",b3,":"," ",z3," ","mm/yr",sep='')
}
in loop first I want to subset data then I want to do the other lines for each of the
Upvotes: 1
Views: 46
Reputation: 319
Something like this?
plyr::ddply(rainfall_wet, plyr::.(Seq), function(m){
FD<-rkt(m$Year,m$sum_rainfall,m$Month,,TRUE)
print(FD)
FD$B
b0<- median(m$sum_rainfall)-(FD$B* median(m$Year))
m$overall_slope<- b0+(FD$B*m$Year)
length(m$Year)
a3<-as.character(m$Year[1])
b3<-as.character(m$Year[length(m$Year)])
z3<-as.character(lapply(FD$B, round,2))
m$`Sens Slpe`<- paste("b)",a3,"-",b3,":"," ",z3," ","mm/yr",sep='')
return(m)
}
)
Upvotes: 1