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. -- View this message in context: http://r.789695.n4.nabble.com/Get-count-by-day-for-particular-coulmn-tp4668915.html Sent from the R help mailing list archive at Nabble.com.
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.
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