如何根据字符串从字符串中创建一个循环以提取行

发布于 2025-01-29 23:21:24 字数 686 浏览 3 评论 0原文

希望根据该行是否包含字符串中列出的任何DateTime邮票并将其添加到新的数据框架中,从而从大数据框架中提取行。

示例数据框架和DateTime邮票的字符串。

df <- data.frame(
    Date = c("2017/05/10 15:00", "2017/05/10 22:00", "2017/05/25 10:00", "2017/05/25 20:00"), 
    value = c(15, 10, 25, 7)
)
df$Date <- as.POSIXlt(df$Date, tz ="GMT", origin = '1970-01-01')

dtlist <- c("2017/05/10 22:00", "2017/05/25 20:00") # desired datetimes

寻找这样的输出:

Date                 Value
2017/05/10 22:00      10
2017/05/25 20:00      7

首先考虑是使用wher()来识别行,但不知道如何整合从dtlist读取变量的条件。

例如哪个(df $ value == dtlist(i))我是列表中的nth对象

Looking to extract rows from a large data frame based on whether the row contains any of the datetime stamps listed in a character string and add them to a new data frame.

Example data frame and character string of datetime stamps.

df <- data.frame(
    Date = c("2017/05/10 15:00", "2017/05/10 22:00", "2017/05/25 10:00", "2017/05/25 20:00"), 
    value = c(15, 10, 25, 7)
)
df$Date <- as.POSIXlt(df$Date, tz ="GMT", origin = '1970-01-01')

dtlist <- c("2017/05/10 22:00", "2017/05/25 20:00") # desired datetimes

Looking for output like this:

Date                 Value
2017/05/10 22:00      10
2017/05/25 20:00      7

First thought was to use which() to identify the rows but don't know how to incorporate the condition of reading the variables from dtlist.

e.g. which(df$value == dtlist(i)) where i is nth object in the list

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文