[R] Counting number of rain

Dan D ddalthorp at usgs.gov
Tue Sep 8 16:37:05 CEST 2015

```Try the following:

## step 1: write raw data to an array
junk<-scan('clipboard')
# entering the numbers (not the 'year' etc. labels) into R as a vector after

junk<-t(array(junk,dim=c(4,length(junk)/4)))
# convert the vector into a 2-d array with 4 columns (year, month, day,
amount)

## step 2: create a dataframe to store and display the results
nyr<-length(unique(junk[,1]))
ans<-data.frame(array(dim=c(nyr,12))) # a dataframe for storing the results
names(ans)<-c('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
yrs<-sort(unique(junk[,1]))
row.names(ans)<-yrs

# step 3: calculate
for (yi in 1:nyr){ # loop through the years...
for (mi in 1:12){ # ...and the months
ans[yi,mi]<-sum(junk[junk[,1]==yrs[yi] & junk[,2]==mi,4]>0.000001) #
count the rainy days by
# first subsetting the junk array by rows that match the given year and
month and sum
}
}

Does that help?

- Dan

--
View this message in context: http://r.789695.n4.nabble.com/Counting-number-of-rain-tp4712007p4712011.html
Sent from the R help mailing list archive at Nabble.com.

```