[R] Count number in r

Hafizuddin Arshad hafizuddinarshad21 at gmail.com
Tue May 19 12:10:32 CEST 2015


Dear R users,

Could someone help me on this? I have this kind of data set:

structure(list(Year = c(1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L), Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), Rain = c(58.9, 74.6, 17.7, 7.8, 1.2, 1, 5.3, 0.7,
1.2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 10.4, 17.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Year",
"Month", "Rain"), class = "data.frame", row.names = c(NA, -44L
))

I want to count data in "Rain" that is greater and equal to 0.1 mm
according to their "Month" and "Year". I have used this code, but it seems
so wrong.

raindat <- read.csv('my data set',header=TRUE)
yearcorr<-min(raindat$Year)-1
years<-unique(raindat$Year)
rainmonth<-as.data.frame(matrix(0,nrow=2,ncol=12))
for(year in years) {
  for(month in 1:12) {
    if(any(raindat$Year==year&raindat$Month==month))
      rainmonth[year-yearcorr,month]<-
      length((which(raindat$Rain >=
0.1))[raindat$Year==year&raindat$Month==month])
  }
}
rownames(rainmonth)<-years
names(rainmonth)<-month.abb
rainmonth

Thank you so much.


Arshad

	[[alternative HTML version deleted]]



More information about the R-help mailing list