英文:
Separate data using using the seconds of a date
问题
我有一个包含不同ID和名称的数据框。这里有一个小例子
library(dplyr)
dframefull <- data.frame(id = c(1,1,1,1,1,1), name = c("Google", "Google", "Google", "Google", "Google", "Google"), date = c("26/8/2014 19:30:57", "27/8/2014 10:12:01", "27/8/2014 14:10:29", "27/8/2014 14:10:32", "27/8/2014 14:10:33", "3/9/2014 14:10:32"), mytext = c("text", "another", "text", "here", "other text", "text more"))
dframefull <- mutate(dframefull, date = as.Date(date))
我有一个第二个数据框,其中包含特定日期。像这样
dframekeep <- data.frame(id = c(1), name = c("Google"), date = c("27/8/2014 14:10:32"))
dframekeep <- mutate(dframekeep, date = as.Date(date))
基于第二个数据框的特定日期,我想在第二个数据框日期之前的10天内将数据合并为一行,并在确切的秒数(在示例中为:32)之后的10天内将其他数据合并为一行。
这是期望输出的示例。
data.frame(id = c(1,1), label = c("before", "after"), name = c("Google", "Google"), mytext = c("text another text here", "other text text more"))
id label name mytext
1 1 before Google text another text here
2 1 after Google other text text more
如何实现这一点,请参考这里的解决方案。
英文:
I have a dataframe with different id and names. Here a small example
library(dplyr)
dframefull <- data.frame(id = c(1,1,1,1,1,1), name = c("Google", "Google", "Google", "Google", "Google", "Google"), date = c("26/8/2014 19:30:57", "27/8/2014 10:12:01", "27/8/2014 14:10:29", "27/8/2014 14:10:32", "27/8/2014 14:10:33", "3/9/2014 14:10:32"), mytext = c("text", "another", "text", "here", "other text", "text more"))
dframefull <- mutate(dframefull, date = as.Date(date))
I have a second dataframe which has specific dates. Like this
dframekeep <- data.frame(id = c(1), name = c("Google"), date = c("27/8/2014 14:10:32"))
dframekeep <- mutate(dframekeep, date = as.Date(date))
Based on the specific dates I would like to keep for 10 days before the date of the second dataframe the data into one row and after the exact second (:32 in the example) the other data for 10 days after.
Here an example of the expected output.
data.frame(id = c(1,1), label = c("before", "after"), name = c("Google", "Google"), mytext = c("text another text here", "other text text more"))
id label name mytext
1 1 before Google text another text here
2 1 after Google other text text more
How can I make it. This is a solution for days
答案1
得分: 1
使用aggregate
在也可以使用I()
进行比较的情况下。
a <- aggregate(mytext ~ id + name + I(date > dframekeep$date), dframefull, paste)
a
# id name I(date > dframekeep$date) mytext
# 1 1 Google FALSE text, another, text, here
# 2 1 Google TRUE other text, text more
为了一步获得正确的名称和标签,您可以使用经典的列表表示法。
b <- with(dframefull,
aggregate(list(mytext=mytext),
by=list(id=id,
label=factor(I(date > dframekeep$date), labels=c("before", "after")),
name=name),
FUN=paste))
b
# id label name mytext
# 1 1 before Google text, another, text, here
# 2 1 after Google other text, text more
英文:
Use aggregate
where also comparisons are possible using I()
.
a <- aggregate(mytext ~ id + name + I(date > dframekeep$date), dframefull, paste)
a
# id name I(date > dframekeep$date) mytext
# 1 1 Google FALSE text, another, text, here
# 2 1 Google TRUE other text, text more
To get proper names and labels in one step you can use classical list notation.
b <- with(dframefull,
aggregate(list(mytext=mytext),
by=list(id=id,
label=factor(I(date > dframekeep$date), labels=c("before", "after")),
name=name),
FUN=paste))
b
# id label name mytext
# 1 1 before Google text, another, text, here
# 2 1 after Google other text, text more
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论