Skip to content
Prev 304917 / 398502 Next

Summarizing data containing data/time information (as factor)

HI HJ,
No problem.


The gsub() was used to just format your date column to make almost the way you wanted the results:
dat2$date
# [1] "2012-04-29" "2012-04-30" "2012-05-01" "2012-04-28" "2012-04-29"
?#[6] "2012-04-30" "2012-05-01" "2012-05-02" "2012-04-30" "2012-05-01"



gsub(".*-(.*)-(.*)","\\2/\\1",dat2$date)
?#[1] "29/04" "30/04" "01/05" "28/04" "29/04" "30/04" "01/05" "02/05" "30/04"
#[10] "01/05"


#Suppose I just reverse the contents inside the second " ".
gsub(".*-(.*)-(.*)","\\1/\\2",dat2$date)
?#[1] "04/29" "04/30" "05/01" "04/28" "04/29" "04/30" "05/01" "05/02" "04/30"
#[10] "05/01"
#From the p.s., I guess you understand the difference.

#The contents inside the first " " are the pattern we match, and the second " " are the replacement.? 

#Now, the ".*-(.*)-(.*)" -first .* will match all the contents before "-" in dat2$date.? I used brackets (.*) for the second and third so that it could be select those specifically and use it #as replacement \\1 and \\2 inside the second " ". 


#If you want to learn regular expressions, there are a lot of resources on the net.
#For example:
http://www.regular-expressions.info/
http://gnosis.cx/publish/programming/regular_expressions.html

A.K.
Message-ID: <1346946085.58842.YahooMailNeo@web142605.mail.bf1.yahoo.com>
In-Reply-To: <CAOEXkoAjF8GHUQYyP62BLvRmVTTpzQDEaDV_Whj9kzcNFFMpcA@mail.gmail.com>