使用日期的秒数分隔数据。

huangapple go评论63阅读模式
英文:

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 &lt;- data.frame(id = c(1,1,1,1,1,1), name = c(&quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;), date = c(&quot;26/8/2014 19:30:57&quot;, &quot;27/8/2014 10:12:01&quot;, &quot;27/8/2014 14:10:29&quot;, &quot;27/8/2014 14:10:32&quot;, &quot;27/8/2014 14:10:33&quot;, &quot;3/9/2014 14:10:32&quot;), mytext = c(&quot;text&quot;, &quot;another&quot;, &quot;text&quot;, &quot;here&quot;, &quot;other text&quot;, &quot;text more&quot;))

dframefull &lt;- mutate(dframefull, date = as.Date(date))

I have a second dataframe which has specific dates. Like this

dframekeep &lt;- data.frame(id = c(1), name = c(&quot;Google&quot;), date = c(&quot;27/8/2014 14:10:32&quot;))
dframekeep &lt;- 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(&quot;before&quot;, &quot;after&quot;), name = c(&quot;Google&quot;, &quot;Google&quot;), mytext = c(&quot;text another text here&quot;, &quot;other text text more&quot;))
  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 &lt;- aggregate(mytext ~ id + name + I(date &gt; dframekeep$date), dframefull, paste)
a
#   id   name I(date &gt; 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 &lt;- with(dframefull, 
          aggregate(list(mytext=mytext), 
                    by=list(id=id, 
                            label=factor(I(date &gt; dframekeep$date), labels=c(&quot;before&quot;, &quot;after&quot;)), 
                            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

huangapple
  • 本文由 发表于 2020年1月6日 20:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定