R 对数据帧进行过滤和子集化

发布于 2024-12-02 02:29:21 字数 163 浏览 1 评论 0原文

请就以下几点给我一些建议。

有一个三列的数据框,第一列是日期时间(精确到秒),第二列是人名,第三列是消息。

我想检索以下信息并绘制它们。

  1. 每人在某一分钟内的消息数。
  2. 某个人在某一分钟内的消息。

提前致谢。

Please give me some 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.

  1. Messages per person during a certain minute.
  2. A certain person's messages during a certain minute.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2024-12-09 02:29:21

R 中有许多不同的日期格式。如果您对其中一种没有偏好,请使用 lubridate 包。

library(lubridate)

一些示例数据:

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]
)

选择一个有趣的时刻:

start_time <- floor_date(the_present, unit = "min")
end_time <- ceiling_date(the_present, unit = "min")

使用 subsettable 来解决您的问题。

table(subset(dfr, time > start_time & time <= end_time, person))
subset(dfr, person == "Richie" & time > start_time & time <= end_time)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文