[R] Get count by day for particular coulmn

Sarah Goslee sarah.goslee at gmail.com
Fri Jun 7 16:30:34 CEST 2013


Hi,

On Fri, Jun 7, 2013 at 7:38 AM, R_Antony <antony.akkara at ge.com> wrote:
> 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
>
> =============================
> =============================

Since you didn't provide reproducible data (dput() is great for that),
here's an example with fake data:


MyDF <- data.frame(DATETIME = c("1/1/2007", "1/1/2007", "1/1/2007",
"1/2/2007", "1/2/2007", "1/2/2007", "1/3/2007", "1/3/2007",
"1/3/2007"),
COL_A = c(0, 0, 0, 1, 0, 1, 2, 3, 1),
COL_B = c(0, 3, 0, 3, 3, 1, 0, 1, 2),
COL_C = c(1, 2, 3, 1, 2, 3, 1, 2, 3), stringsAsFactors=FALSE)

aggregate(COL_B ~ DATETIME, data=MyDF, FUN=function(x)sum(x == 3))


> 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.

You omitted a comma:
MyDF[MyDF["DATETIME"]=="1/2/2007", ]


You might prefer:
subset(MyDF, DATETIME == "1/2/2007")

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list