[R] Get count by day for particular coulmn

arun smartpink111 at yahoo.com
Fri Jun 7 15:54:11 CEST 2013


HI,
Please dput() the example data.
"""

Could anyone give a help ASAP ?"""""

You have been posting for long time and your many posts show this "ASAP". I know that you got comments to that and also advised to dput() the data.  Formatting your data took some time.  Would it be better to follow the posting guidelines and then use "ASAP"?

dat1<- read.table(text="
DATETIME,COL_A,COL_B,COL_C,COL_D
1/1/2007 0:01,0,3,0,0
1/1/2007 0:02,0,0,3,0
1/1/2007 0:03,0,3,0,0
1/2/2007 0,0,3,0,0
1/2/2007 0:01,0,3,4,0
1/2/2007 0:02,0,3,0,0
1/3/2007 0,0,0,0,0
1/3/2007 0:01,0,0,4,0
1/3/2007 0:02,0,3,0,0
",sep=",",header=TRUE,stringsAsFactors=FALSE)
dat1$DATETIME[!grepl("\\:",dat1$DATETIME)]<-paste0(dat1$DATETIME[!grepl("\\:",dat1$DATETIME)],":00")
res<-aggregate(.~gsub("\\s+.*","",dat1$DATETIME),data=dat1[,-1],function(x) length(x[x==3]))
 colnames(res)[1]<- colnames(dat1)[1]
colnames(res)[-1]<-paste0("COUNT(",colnames(res)[-1],"=3)")
 res
#  DATETIME COUNT(COL_A=3) COUNT(COL_B=3) COUNT(COL_C=3) COUNT(COL_D=3)
#1 1/1/2007              0              2              1              0
#2 1/2/2007              0              3              0              0
#3 1/3/2007              0              1              0              0
A.K.

   



here i have a dataframe 

for eg:- 
DATETIME	COL_A	COL_B	COL_C	COL_D 
1/1/2007 0:01	0	3	0	0 
1/1/2007 0:02	0	0	3	0 
1/1/2007 0:03	0	3	0	0 
.......................       .....             ...             ...              .... 
1/2/2007	0	0	3	0 
1/2/2007 0:01	0	3	4	0 
1/2/2007 0:02	0	3	0	0 
.......................       .....             ...             ...              .... 
1/3/2007	0	0	0	0 
1/3/2007 0:01	0	0	4	0 
1/3/2007 0:02	0	3	0	0 
.......................       .....             ...             ...              .... 

My requirement what is, i have to get the count for each "day " where COL_B = 3 

For eg:- here i need to get like 
DATETIME           COUNT(COL_B=3) 
------------            ------------ 
1/1/2007               2 
1/2/2007               3 
1/3/2007               1 

============================= 
============================= 

and this way i tried to get, 
MyDF[MyDF["DATETIME"]=="1/2/2007"]  ---> here this only select the row where DATETIME - column coming as 
"1/2/2007" - date and not selecting other rows where same date is 
coming (eg:- 1/1/2007 0:01). And here i need to get the complete records
 for that particular day, when i give date without giving timestamp. 

- Could anyone give a help ASAP ? 

- Thanks 
Antony. 




More information about the R-help mailing list