Skip to content

Count number in r

6 messages · Hafizuddin Arshad, Jorge I Velez, David L Carlson +2 more

#
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
#
Dear Arshad,

Here is a possibility using tapply():

with(d, tapply(Rain, list(Month, Year), function(x) sum(x > .1)))
##1971
#1   12
#2    0


where "d" is your data.frame().   See also ?aggregate and ?ave.

Best,
Jorge.-


On Tue, May 19, 2015 at 8:10 PM, Hafizuddin Arshad <
hafizuddinarshad21 at gmail.com> wrote:

            

  
  
#
Here's an approach using xtabs() if you want the output as a table:
Month
Year    1  2
  1971 12  0

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jorge I Velez
Sent: Tuesday, May 19, 2015 8:21 AM
To: Hafizuddin Arshad
Cc: R Help
Subject: Re: [R] Count number in r

Dear Arshad,

Here is a possibility using tapply():

with(d, tapply(Rain, list(Month, Year), function(x) sum(x > .1)))
##1971
#1   12
#2    0


where "d" is your data.frame().   See also ?aggregate and ?ave.

Best,
Jorge.-


On Tue, May 19, 2015 at 8:10 PM, Hafizuddin Arshad <
hafizuddinarshad21 at gmail.com> wrote:

            
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
And another approach just for the heck of it.

library(plyr)

# where dat1 is your data 
dd1  <-  subset(dat1, Rain >= .01)

dd1$Year  <-  as.factor(dd1$Year)
dd1$Month  <-  as.factor(dd1$Month)

count (dd1, .(Year, Month))

John Kane

Kingston ON Canada
____________________________________________________________
Can't remember your password? Do you need a strong and secure password?
Use Password manager! It stores your passwords & protects your account.
#
Dear all,

I am kindly requesting for help on how I can count pixels with value less
and equal to -0.08 for a raster stack.

Thanks for your help

John
On Tue, May 19, 2015 at 5:57 PM, John Kane <jrkrideau at inbox.com> wrote:

            

  
    
#
If nothing suggested in this thread help I'd suggest asking in R-sig-Geo where they will be more familiar with the issues.

Please do not post in HTML. It can serious mangle code to the point it is indecipherable.  

John Kane
Kingston ON Canada

-----Original Message-----
From: johnwasige at gmail.com
Sent: Tue, 19 May 2015 18:26:35 +0200
To: jrkrideau at inbox.com
Subject: Re: [R] Count number in r

Dear all,

I am kindly requesting for help on how I can count pixels with value less and equal to -0.08 for a raster stack.

Thanks for your help

John
On Tue, May 19, 2015 at 5:57 PM, John Kane <jrkrideau at inbox.com> wrote:
And another approach just for the heck of it.

 library(plyr)

 # where dat1 is your data
 dd1? <-? subset(dat1, Rain >= .01)

 dd1$Year? <-? as.factor(dd1$Year)
 dd1$Month? <-? as.factor(dd1$Month)

 count (dd1, .(Year, Month))

 John Kane

 Kingston ON Canada

 > -----Original Message-----
 > From: hafizuddinarshad21 at gmail.com
 > Sent: Tue, 19 May 2015 03:10:32 -0700
 > To: r-help at r-project.org
 > Subject: [R] Count number in r
 >
 > 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]]
 >
 > ______________________________________________
 > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
 > https://stat.ethz.ch/mailman/listinfo/r-help [https://stat.ethz.ch/mailman/listinfo/r-help]
 > PLEASE do read the posting guide
 > http://www.R-project.org/posting-guide.html [http://www.R-project.org/posting-guide.html]
 > and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
 Can't remember your password? Do you need a strong and secure password?
 Use Password manager! It stores your passwords & protects your account.

 ______________________________________________
 R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
 https://stat.ethz.ch/mailman/listinfo/r-help [https://stat.ethz.ch/mailman/listinfo/r-help]
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html [http://www.R-project.org/posting-guide.html]
 and provide commented, minimal, self-contained, reproducible code.