I have dataframe like this
date apps long
10/22/2013 23:51 A 2
10/22/2013 23:52 B 3
10/22/2013 23:52 C 1
10/23/2013 7:03 C 5
10/23/2013 7:13 A 1
10/23/2013 7:31 B 4
10/23/2013 7:31 A 5
10/23/2013 7:31 B 2
10/24/2013 0:54 B 3
10/24/2013 1:16 C 2
10/24/2013 1:16 C 1
10/24/2013 3:27 A 2
10/24/2013 7:30 A 3
10/24/2013 7:30 A 1
The problems that i have is : I want to sum how long A, B, C apps spent time for each days. so the output will looks like:
A 10/22/2013 2
A 10/23/2013 6
A 10/24/2013 6
etc...
I've tried some syntax but it did not work.
First, i'm assuming your data.frame is called dd
. Here it is in a copy/pasteable form
dd <- structure(list(date = structure(c(1L, 2L, 2L, 3L, 4L, 5L, 5L,
5L, 6L, 7L, 7L, 8L, 9L, 9L), .Label = c("10/22/2013 23:51", "10/22/2013 23:52",
"10/23/2013 7:03", "10/23/2013 7:13", "10/23/2013 7:31", "10/24/2013 0:54",
"10/24/2013 1:16", "10/24/2013 3:27", "10/24/2013 7:30"), class = "factor"),
apps = structure(c(1L, 2L, 3L, 3L, 1L, 2L, 1L, 2L, 2L, 3L,
3L, 1L, 1L, 1L), .Label = c("A", "B", "C"), class = "factor"),
long = c(2L, 3L, 1L, 5L, 1L, 4L, 5L, 2L, 3L, 2L, 1L, 2L,
3L, 1L)), .Names = c("date", "apps", "long"), class = "data.frame", row.names = c(NA,
-14L))
You should convert your dates to a proper date value
dd$date <- as.POSIXct(as.character(dd$date), format="%m/%d/%Y %H:%M", tz="GMT")
Then you can create a nice data.frame with aggregate
, here using as.Date
to strip off time
aggregate(long ~ as.Date(date) + apps, dd, FUN=sum)
This returns
as.Date(date) apps long
1 2013-10-22 A 2
2 2013-10-23 A 6
3 2013-10-24 A 6
4 2013-10-22 B 3
5 2013-10-23 B 6
6 2013-10-24 B 3
7 2013-10-22 C 1
8 2013-10-23 C 5
9 2013-10-24 C 3