I am trying to use the aggregate function to get 100 Hz data into 1 minute averages. However, when I use this function the 1-min averages are incorrect. A sample of the data is below. I am using the following code to calculate the 1-min values. The code does not break but the calculations are incorrect.
aggregate(list(X = df$`Gyroscope X`,
Y = df$`Gyroscope Y`,
Z = df$`Gyroscope Z`),
list(minofday = cut(df$Timestamp, "1 min")),mean)
Timestamp Gyroscope X Gyroscope Y Gyroscope Z
2018-07-10T10:25:00.0000000 41.381838 -21.667482 -118.896492
2018-07-10T10:25:00.0100000 48.046268 -12.399903 -110.917976
2018-07-10T10:25:00.0200000 49.102786 -7.36084 -106.485602
2018-07-10T10:25:00.0300000 44.338382 -9.215699 -102.296759
2018-07-10T10:25:00.0400000 34.724123 -11.308594 -96.108404
2018-07-10T10:25:00.0500000 19.622804 -15.225221 -88.122564
2018-07-10T10:25:00.0600000 13.240968 -26.539308 -85.274663
2018-07-10T10:25:00.0700000 13.397218 -31.933596 -80.127568
2018-07-10T10:25:00.0800000 16.333009 -29.663088 -73.027348
2018-07-10T10:25:00.0900000 17.384645 -29.745485 -67.694096
2018-07-10T10:25:00.1000000 16.546632 -30.08423 -67.565922
Assuming OP's data varies by the min (note the modified data), here is how to do it with base R and dplyr:
df$Timestamp <- as.POSIXct(df$Timestamp, format = "%Y-%m-%dT%H:%M:%S")
aggregate(list(X = df$Gyroscope_X,
Y = df$Gyroscope_Y,
Z = df$Gyroscope_Z),
list(minofday = cut(df$Timestamp, "1 min")), mean)
or a more concise way:
aggregate(. ~ minofday, mean, data = cbind(setNames(df[,-1], c("X", "Y", "Z")),
minofday = cut(df$Timestamp, "1 min")))
Result:
minofday X Y Z
1 2018-07-10 10:24:00 48.57453 -9.880371 -108.70179
2 2018-07-10 10:25:00 27.78422 -19.314983 -95.13774
3 2018-07-10 10:26:00 16.85883 -29.704286 -70.36072
4 2018-07-10 10:27:00 16.54663 -30.084230 -67.56592
With lubridate and summarize_all from dplyr:
library(dplyr)
library(lubridate)
df %>%
mutate(Timestamp = ymd_hms(Timestamp)) %>%
group_by(minofday = cut(Timestamp, "1 min")) %>%
summarize_all(mean) %>%
select(-Timestamp)
Result:
# A tibble: 4 x 4
minofday Gyroscope_X Gyroscope_Y Gyroscope_Z
<fct> <dbl> <dbl> <dbl>
1 2018-07-10 10:24:00 48.6 -9.88 -109.
2 2018-07-10 10:25:00 27.8 -19.3 -95.1
3 2018-07-10 10:26:00 16.9 -29.7 -70.4
4 2018-07-10 10:27:00 16.5 -30.1 -67.6
Data:
df <- read.table(text = " Timestamp Gyroscope_X Gyroscope_Y Gyroscope_Z
2018-07-10T10:25:00.0000000 41.381838 -21.667482 -118.896492
2018-07-10T10:24:00.0100000 48.046268 -12.399903 -110.917976
2018-07-10T10:24:00.0200000 49.102786 -7.36084 -106.485602
2018-07-10T10:25:00.0300000 44.338382 -9.215699 -102.296759
2018-07-10T10:25:00.0400000 34.724123 -11.308594 -96.108404
2018-07-10T10:25:00.0500000 19.622804 -15.225221 -88.122564
2018-07-10T10:25:00.0600000 13.240968 -26.539308 -85.274663
2018-07-10T10:25:00.0700000 13.397218 -31.933596 -80.127568
2018-07-10T10:26:00.0800000 16.333009 -29.663088 -73.027348
2018-07-10T10:26:00.0900000 17.384645 -29.745485 -67.694096
2018-07-10T10:27:00.1000000 16.546632 -30.08423 -67.565922", header = TRUE)
Since you are dealing with timestamps the xts package has a lot of functions that can help you. For rolling up timestamps period.apply can help you out. The endpoints part can roll up the data from microseconds all the way up to years.
# don't load the timestamp column that one goes to the order.by part
df1_xts <- xts(df1[, -1], order.by = df1$Timestamp)
# roll up to seconds.
period.apply(df1_xts, endpoints(df1_xts, on = "mins"), colMeans)
Gyroscope_X Gyroscope_Y Gyroscope_Z
2018-07-10 10:25:00 28.55624 -20.46759 -90.59249
If you timestamp column is not yet a date time object you can use this:
df1$Timestamp <- strptime(df1$Timestamp, format = "%Y-%m-%dT%H:%M:%OS")
data:
df1 <- structure(list(Timestamp = structure(list(sec = c(0, 0.01, 0.02,
0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1), min = c(25L,
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L), hour = c(10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), mday = c(10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), mon = c(6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), year = c(118L, 118L,
118L, 118L, 118L, 118L, 118L, 118L, 118L, 118L, 118L), wday = c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), yday = c(190L, 190L,
190L, 190L, 190L, 190L, 190L, 190L, 190L, 190L, 190L), isdst = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), zone = c("CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST"), gmtoff = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_)), class = c("POSIXlt", "POSIXt")),
Gyroscope_X = c(41.381838, 48.046268, 49.102786, 44.338382,
34.724123, 19.622804, 13.240968, 13.397218, 16.333009, 17.384645,
16.546632), Gyroscope_Y = c(-21.667482, -12.399903, -7.36084,
-9.215699, -11.308594, -15.225221, -26.539308, -31.933596,
-29.663088, -29.745485, -30.08423), Gyroscope_Z = c(-118.896492,
-110.917976, -106.485602, -102.296759, -96.108404, -88.122564,
-85.274663, -80.127568, -73.027348, -67.694096, -67.565922
)), row.names = c(NA, -11L), class = "data.frame")
Related
I have a data frame with date column in UTC time zone, I want to create another column which convert my UTC time zone wrt to the states time zone.
I have 1000's of rows with different states of USA.
My proxy data frame is mentioned below
df<-structure(list(UTC_date = structure(list(sec = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(14L, 21L, 17L, 38L, 45L, 25L, 44L, 11L,09L, 27L), hour = c(3L, 0L, 16L, 16L, 17L, 8L, 17L, 1L, 2L, 4L),
mday = c(2L, 2L, 15L, 12L, 19L, 18L, 25L, 17L, 07L, 17L),
mon = c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L),
year = c(121L,121L, 121L, 121L, 121L, 121L, 121L, 121L, 121L, 121L),
wday = c(1L,4L, 1L, 1L, 1L, 4L, 1L, 3L, 3L, 3L),
yday = c(297L, 300L,297L, 297L, 297L, 300L, 297L, 299L, 299L, 299L), isdst = c(1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
zone = c("CDT", "CDT","CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT"),
gmtoff = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_)), class = c("POSIXlt", "POSIXt")), StoreState = c("TX","MS", "AZ", "SC", "WI", "MO", "TX", "TX", "MO", "TX")),
row.names = c(NA,10L), class = "data.frame")
str(df)
head(df)
What I am looking for is
below o/p as an example for the 1st row
Input Example
UTC_Date State
2021-10-02 03:14:00 TX
Output Example
UTC_Date State Local Time as per State
2021-10-02 03:14:00 TX 2021-10-02 21:14:00
You can use the force_tzs function to convert to the corresponding time zones(tz).
library(lubridate)
df %>%
mutate(tz = case_when(
StoreState=="AZ"~ "US/Mountain",
StoreState=="SC"~ "US/Eastern",
TRUE ~ "US/Central"
)) %>%
mutate('Local Time as per State'= force_tzs(UTC_date, tzones = tz)) %>%
select(UTC_date, State= StoreState, 'Local Time as per State')
I have two fields in a dataframe that are of the class "times". Call it Time1 and Time2. I am trying to find the time difference between the two.
CombinedFrame2$Duration <- difftime(CombinedFrame2$Time1, CombinedFrame2$Time2)
Error in as.POSIXct.numeric(CombinedFrame2$Time1) :
'origin' must be supplied
How do I get the classes to cooperate to do the calculation?
Example:
Time1 Time2 Duration
5:30:00 6:24:00 0:54:00
$ Time1 : POSIXlt, format: "2019-07-10 16:07:00" "2019-07-10 22:05:00" "2019-07-10 22:20:00" "2019-07-10 22:43:00" ...
$ Time2 : POSIXlt, format: "2019-07-10 22:05:00" "2019-07-10 22:20:00" "2019-07-10 22:43:00" "2019-07-10 23:15:00" ...
> dput(head(CombinedFrame2[,c("Time1", "Time2")]))
structure(list(Time1 = structure(list(sec = c(0, 0, 0, 0,
0, 0), min = c(7L, 5L, 20L, 43L, 15L, 35L), hour = c(16L, 22L,
22L, 22L, 23L, 23L), mday = c(11L, 11L, 11L, 11L, 11L, 11L),
mon = c(6L, 6L, 6L, 6L, 6L, 6L), year = c(119L, 119L, 119L,
119L, 119L, 119L), wday = c(4L, 4L, 4L, 4L, 4L, 4L), yday = c(191L,
191L, 191L, 191L, 191L, 191L), isdst = c(1L, 1L, 1L, 1L,
1L, 1L), zone = c("EDT", "EDT", "EDT", "EDT", "EDT", "EDT"
), gmtoff = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_)), class = c("POSIXlt", "POSIXt"
)), Time2 = structure(list(sec = c(0, 0, 0, 0, 0, 0), min = c(5L,
20L, 43L, 15L, 35L, 55L), hour = c(22L, 22L, 22L, 23L, 23L, 23L
), mday = c(11L, 11L, 11L, 11L, 11L, 11L), mon = c(6L, 6L, 6L,
6L, 6L, 6L), year = c(119L, 119L, 119L, 119L, 119L, 119L), wday = c(4L,
4L, 4L, 4L, 4L, 4L), yday = c(191L, 191L, 191L, 191L, 191L, 191L
), isdst = c(1L, 1L, 1L, 1L, 1L, 1L), zone = c("EDT", "EDT",
"EDT", "EDT", "EDT", "EDT"), gmtoff = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), class = c("POSIXlt",
"POSIXt"))), row.names = c("1:1", "1:2", "1:3", "1:4", "1:5",
"1:6"), class = "data.frame")
You need to make sure that your time is formatted correctly. See the code below.
You can use strptime() to format your time into hours, minutes, and seconds.
time1 <- "5:30:00"
time2 <- "6:24:00"
time1a <- strptime(time1,format="%H:%M:%S")
time2a <- strptime(time2,format="%H:%M:%S")
duration <- difftime(time2a,time1a)
I'm tried to use lag on a column of a data frame but when time is involved it just wont work. I've tried shift, lag and tlag.
Example:
y = strptime(sprintf("%s:%s:%s", 4, 20, 10), "%H:%M:%S")
yy = strptime(sprintf("%s:%s:%s", 10, 20, 10), "%H:%M:%S")
lag(c(y,yy))
Error in format.POSIXlt(x, usetz = usetz) :
invalid component [[10]] in "POSIXlt" should be 'zone'
tlag(c(y,yy))
Error in n_distinct_multi(list(...), na.rm) :
argument "time" is missing, with no default
shift(c(y,yy))
[[1]]
[1] NA 10
[[2]]
[1] NA 20
[[3]]
[1] NA 4
[[4]]
[1] NA 4
[[5]]
[1] NA 6
[[6]]
[1] NA 117
[[7]]
[1] NA 2
[[8]]
[1] NA 184
[[9]]
[1] NA 1
[[10]]
[1] NA "BST"
[[11]]
[1] NA 3600
I don't want any time differences, I simply want the value from the row above in my data frame, which I thought was what lag did: "Lead and lag are useful for comparing values offset by a constant (e.g. the previous or next value)".
The time shouldn't even matter, it should just choose whatever numeric/character/time from the previous position. How do I fix this or is there a different function that does the equivalent of what I'd like - I do not want to involve any loops as speed is important and the data frames are large.
Example from my dataframe:
structure(list(sec = c(52, 53, 54, 55, 56, 57, 58, 59, 0, 1),
min = c(50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 51L, 51L),
hour = c(11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L
), mday = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), mon = c(6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), year = c(117L, 117L,
117L, 117L, 117L, 117L, 117L, 117L, 117L, 117L), wday = c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), yday = c(184L, 184L,
184L, 184L, 184L, 184L, 184L, 184L, 184L, 184L), isdst = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), zone = c("BST", "BST",
"BST", "BST", "BST", "BST", "BST", "BST", "BST", "BST"),
gmtoff = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_)), .Names = c("sec", "min", "hour", "mday", "mon",
"year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt",
"POSIXt"))
For a data.frame like below
index time
1 1 2017-07-04 04:20:10
2 2 2017-07-04 10:20:10
you can use dplyr
dplyr::lag(df$time, 1)
[1] NA "2017-07-04 04:20:10 CEST"
dplyr::lead(df$time, 1)
[1] "2017-07-04 10:20:10 CEST" NA
And to add the lead/lag column to your data.frame you can use
dplyr::mutate(df, lead_1 = dplyr::lead(time, 1), lag_1 = dplyr::lag(time, 1))
index time lead_1 lag_1
1 1 2017-07-04 04:20:10 2017-07-04 10:20:10 <NA>
2 2 2017-07-04 10:20:10 <NA> 2017-07-04 04:20:10
Let's say I have a data frame with start and end time columns, a measurement column and a time of measurement column, like so:
start end value time
9:01:00 9:02:00 30.6 2013-03-25 9:05:00
9:01:00 9:02:00 30.8 2013-03-25 9:15:00
9:46:00 9:46:00 28.2 2013-03-25 9:43:00
9:46:00 9:46:00 28.9 2013-03-25 9:53:00
10:54:00 10:59:00 13.4 2013-03-25 10:56:00
10:54:00 10:59:00 13.8 2013-03-25 11:56:00
How might one subset this data frame to include only rows for which the time column is within the start and end time or ten minutes before the start time and ten minutes after the end time. I'm choosing ten minutes arbitrarily, and would like to know how to do this for any amount of time before and after the start and end time.
The resulting data frame would be as follows:
start end value time
9:01:00 9:02:00 30.6 2013-03-25 9:05:00
9:46:00 9:46:00 28.2 2013-03-25 9:43:00
9:46:00 9:46:00 28.9 2013-03-25 9:53:00
10:54:00 10:59:00 13.4 2013-03-25 10:56:00
Is there a way to do this other than by subtracting/adding x number of minutes from the start/end column entries, and then subsetting based on whether or not the time column falls between these expanded windows?
Currently, I have convert my time columns into POSIXlt format. Unfortunately, this gives todays date to the times in the start and end column.
here is the dput for the first data frame:
structure(list(start = structure(list(sec = c(0, 0, 0, 0, 0,
0), min = c(1L, 1L, 46L, 46L, 54L, 54L), hour = c(9L, 9L, 9L,
9L, 10L, 10L), mday = c(7L, 7L, 7L, 7L, 7L, 7L), mon = c(7L,
7L, 7L, 7L, 7L, 7L), year = c(113L, 113L, 113L, 113L, 113L, 113L
), wday = c(3L, 3L, 3L, 3L, 3L, 3L), yday = c(218L, 218L, 218L,
218L, 218L, 218L), isdst = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt")), end = structure(list(sec = c(0,
0, 0, 0, 0, 0), min = c(2L, 2L, 46L, 46L, 59L, 59L), hour = c(9L,
9L, 9L, 9L, 10L, 10L), mday = c(7L, 7L, 7L, 7L, 7L, 7L), mon = c(7L,
7L, 7L, 7L, 7L, 7L), year = c(113L, 113L, 113L, 113L, 113L, 113L
), wday = c(3L, 3L, 3L, 3L, 3L, 3L), yday = c(218L, 218L, 218L,
218L, 218L, 218L), isdst = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt")), value = c(30.6, 30.8, 28.2,
28.9, 13.4, 13.8), time = structure(list(sec = c(0, 0, 0, 0,
0, 0), min = c(5L, 15L, 43L, 53L, 56L, 56L), hour = c(9L, 9L,
9L, 9L, 10L, 11L), mday = c(25L, 25L, 25L, 25L, 25L, 25L), mon = c(2L,
2L, 2L, 2L, 2L, 2L), year = c(113L, 113L, 113L, 113L, 113L, 113L
), wday = c(1L, 1L, 1L, 1L, 1L, 1L), yday = c(83L, 83L, 83L,
83L, 83L, 83L), isdst = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt"))), .Names = c("start", "end",
"value", "time"), row.names = c(NA, -6L), class = "data.frame")
here is the dput for the second data frame
structure(list(start = structure(list(sec = c(0, 0, 0, 0), min = c(1L,
46L, 46L, 54L), hour = c(9L, 9L, 9L, 10L), mday = c(7L, 7L, 7L,
7L), mon = c(7L, 7L, 7L, 7L), year = c(113L, 113L, 113L, 113L
), wday = c(3L, 3L, 3L, 3L), yday = c(218L, 218L, 218L, 218L),
isdst = c(1L, 1L, 1L, 1L)), .Names = c("sec", "min", "hour",
"mday", "mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt",
"POSIXt")), end = structure(list(sec = c(0, 0, 0, 0), min = c(2L,
46L, 46L, 59L), hour = c(9L, 9L, 9L, 10L), mday = c(7L, 7L, 7L,
7L), mon = c(7L, 7L, 7L, 7L), year = c(113L, 113L, 113L, 113L
), wday = c(3L, 3L, 3L, 3L), yday = c(218L, 218L, 218L, 218L),
isdst = c(1L, 1L, 1L, 1L)), .Names = c("sec", "min", "hour",
"mday", "mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt",
"POSIXt")), value = c(30.6, 28.2, 28.9, 13.4), time = structure(list(
sec = c(0, 0, 0, 0), min = c(5L, 43L, 53L, 56L), hour = c(9L,
9L, 9L, 10L), mday = c(25L, 25L, 25L, 25L), mon = c(2L, 2L,
2L, 2L), year = c(113L, 113L, 113L, 113L), wday = c(1L, 1L,
1L, 1L), yday = c(83L, 83L, 83L, 83L), isdst = c(1L, 1L,
1L, 1L)), .Names = c("sec", "min", "hour", "mday", "mon",
"year", "wday", "yday", "isdst"), class = c("POSIXlt", "POSIXt"
))), .Names = c("start", "end", "value", "time"), row.names = c(NA,
-4L), class = "data.frame")
No fun to recreate, but the answer should be as simple as:
data[with(data, time > start - 10*60 & time < end + 10*60),]
That assumes that that start, end and time objects are all actually comparable (i.e. corresponding year and date) - otherwise just convert the substring that corresponds to the time of day to a POSIX.
UPDATE: Ok, since your dates are off, you need to recreate them to "synchronize", e.g.:
data$start <- as.POSIXct(substr(data$start,12,19), format="%H:%M:%S")
data$end <- as.POSIXct(substr(data$end,12,19), format="%H:%M:%S")
data$time <- as.POSIXct(substr(data$time,12,19), format="%H:%M:%S")
Now, the line above gives what you want. Probably, you should be careful about how you encode the POSIX from your raw data off the bat. Also, for most applications a POSIXct might be preferred to the POSIXlt - where each element is a list. This can gum up (or slow down) some operations later down the line.
Building on #EliGurarie's answer:
#dat <- ....see original question
Convert the times to POSIX representations and do the maths:
datestem <- as.character(as.Date(dat$time))
dat$start <- as.POSIXct(paste(datestem,format(dat$start,"%H:%M:%S")))
dat$end <- as.POSIXct(paste(datestem,format(dat$end,"%H:%M:%S")))
dat[
with(
dat,
difftime(start,time,units="mins") > -10 &
difftime(end,time,units="mins") < 10
),
]
Alternatively, use a bit of rounding and some intermediate variables:
min10 <- 10/(60*24)
ds <- difftime(dat$start,dat$time,units="days")
ds <- dd - round(dd)
de <- difftime(dat$end,dat$time,units="days")
de <- de - round(de)
dat[ds > -min10 & de < min10,]
I am a beginner to try R for making graphs. Please help me. I have data of multiple columns (time series). Each column holds factors (please see the one column example data below). I would like to make a constant height (say 1 unit) bar chart of the time series and would like to represent “A” and “B” in different colors with the DATE on the x axis. Any tip?
Thanking you in advance!
DATE GROUP
2011.06.18 00:00:00 R
2011.06.18 06:00:00 L
2011.06.18 12:00:00 R
2011.06.18 18:00:00 R
2011.06.19 00:00:00 L
2011.06.19 06:00:00 L
2011.06.19 12:00:00 R
2011.06.19 18:00:00 L
2011.06.20 00:00:00 L
2011.06.20 06:00:00 L
2011.06.20 12:00:00 R
2011.06.20 18:00:00 L
2011.06.21 00:00:00 R
2011.06.21 06:00:00 L
Assuming your data are in dat, but with an extra column:
dat <- structure(list(DATE = structure(list(sec = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 6L, 12L, 18L, 0L, 6L,
12L, 18L, 0L, 6L, 12L, 18L, 0L, 6L), mday = c(18L, 18L, 18L,
18L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 21L, 21L), mon = c(5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), year = c(111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L), wday = c(6L, 6L, 6L, 6L, 0L, 0L, 0L, 0L, 1L, 1L,
1L, 1L, 2L, 2L), yday = c(168L, 168L, 168L, 168L, 169L, 169L,
169L, 169L, 170L, 170L, 170L, 170L, 171L, 171L), isdst = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt")), GROUP = structure(c(2L, 1L,
2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L), .Label = c("L",
"R"), class = "factor"), GROUP2 = structure(c(1L, 2L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("L", "R"), class = "factor")), .Names = c("DATE",
"GROUP", "GROUP2"), row.names = c(NA, -14L), class = "data.frame")
Then I think this does what you want. First count the elements == to one of the classes
counts <- apply(dat[, 2:3], 1, function(x) sum(x == "R"))
then compute the other count and bind to a matrix:
countmat <- t(cbind(L = (NCOL(dat) - 1) - counts, R = counts))
then we plot using barplot()
op <- par(mar = c(9,4,4,2) + 0.1, las = 2)
mids <- barplot(countmat, ylim = c(0,2.5),
legend.text = c("L","R"),
args.legend = list(x = "top", bty = "n"))
axis(side = 1, at = mids, labels = as.character(dat$DATE))
par(op)
which produces:
See the help pages of the individual functions for explanations on the arguments.
Edit: If you just want to do this for an individual column, then this isn't the most interesting graph, but...
count2 <- with(dat, GROUP == "R")
countmat2 <- t(cbind(R = count2, L = !count2))
op <- par(mar = c(9,4,4,2) + 0.1, las = 2)
mids <- barplot(countmat2, ylim = c(0, 1.5),
legend.text = c("R","L"),
args.legend = list(x = "top", bty = "n"))
axis(side = 1, at = mids, labels = as.character(dat$DATE))
par(op)
which gives this figure: