R Filter and subset a dataframe
Please give me some 开发者_JAVA百科advice on the followings.
There is a data frame with three columns, the first one is a datetime(precise to seconds), the second one is a person's name, and the third one is a message.
I want to retrieve the following information and plot them.
- Messages per person during a certain minute.
- A certain person's messages during a certain minute.
Thanks in advance.
There are lots of different date formats in R. If you haven't got a preference for one of them, then use the lubridate
package.
library(lubridate)
Some sample data:
the_present <- now()
dfr <- data.frame(
person = rep(c("Richie", "user900168"), each = 3),
time = seq(the_present, by = "-1 min", length.out = 6),
message = letters[1:6]
)
Pick an interesting minute:
start_time <- floor_date(the_present, unit = "min")
end_time <- ceiling_date(the_present, unit = "min")
Use subset
and table
to solve your problems.
table(subset(dfr, time > start_time & time <= end_time, person))
subset(dfr, person == "Richie" & time > start_time & time <= end_time)
精彩评论