如何在R tidyverse中将结果就地放回原始数据框?

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

How to inplace the result to the original dataframe in r tidyverse?

问题

我想通过分组的行数来计算日期差异:

v = data.frame(group = c('a','a','b','c','c'),days_difference = c(0,56,0,0,23))

v %>% group_by(group) %>% filter(n() == 1) %>% mutate(days_difference = (difftime(as.Date('01-01-2020', "%d-%m-%Y"),as.Date('04-06-2018',"%d-%m-%Y"))))

在上面的代码中,如果组的长度等于一,则days_difference将从0更改为difftime(as.Date('01-01-2020', "%d-%m-%Y"),as.Date('04-06-2018',"%d-%m-%Y"))

然而,我需要使用虚拟变量将结果插入到原始数据框中。在R中是否有使用inplace的方式?或者如何在mutate函数中使用ifelse

请给我一些建议,谢谢!

英文:

I would like to calculate the day difference by the number of row of groups:

v = data.frame(group = c('a','a','b','c','c'),days_difference = c(0,56,0,0,23))

v %>% group_by(group) %>% filter(n() == 1) %>% mutate(days_difference = (difftime(as.Date('01-01-2020', "%d-%m-%Y"),as.Date('04-06-2018',"%d-%m-%Y"))))

In the above code, if the length of groups is equal to one, then the days_difference will change from 0 to difftime(as.Date('01-01-2020', "%d-%m-%Y"),as.Date('04-06-2018',"%d-%m-%Y")).

However, I need to use dummy variables in order to insert the result into the original data.frame.
Is there anyways to use inplace in r? Or how to use ifelse in mutate function?

Please give me some suggestions, thank you!

答案1

得分: 1

我们可以使用基本的R的ifelse函数:

用法:在mutate之后,我们设置新的列名,然后是=,然后是ifelse()。在ifelse中的参数是:ifelse(条件, 如果为TRUE的结果, 如果为FALSE的结果)。它始终是这个宏。在条件项中,您可以组合多个条件。就像在您的情况下,我们检查 n() == 1 AND days_difference == 0。在完成条件后,我们设置一个逗号。下一个参数是如果这个条件在您的情况下为TRUE会发生什么,difftime(as.Date('01-01-2020', '%d-%m-%Y'), as.Date('04-06-2018',''%d-%m-%Y'')),然后我们再次设置一个逗号,最后一个参数是如果条件在您的情况下为FALSE会发生什么(days_difference列中的值)。

在您的控制台中键入?ifelse并查看文档。

library(dplyr)
v %>%
  group_by(group) %>%
  mutate(
    days_difference = ifelse(n() == 1 & days_difference == 0,
                             difftime(as.Date('01-01-2020', '%d-%m-%Y'), as.Date('04-06-2018','%d-%m-%Y')),
                             days_difference)
  )

  group days_difference
  <chr>           <dbl>
1 a                   0
2 a                  56
3 b                 576
4 c                   0
5 c                  23
英文:

We could use base R's ifelse:

Usage: after mutate we set the new column name then = and then ifelse(). Within ifelse the arguments are: ifelse(condition, result if TRUE, result if FALSE). It is always this macro. In the condition term you can combine multiple conditions. Like in your case we check n() == 1 AND days_difference == 0. After doing the condition we set a coma. The next argument is what should happen if this condition is TRUE in your case difftime(as.Date(&#39;01-01-2020&#39;, &quot;%d-%m-%Y&quot;), as.Date(&#39;04-06-2018&#39;,&quot;%d-%m-%Y&quot;)) then we set again a coma and the last argument is what will happen if the condition is FALSE in your case (the values in days_difference column.

Type ?ifelse in your console and see the documentation.

library(dplyr)
v %&gt;% 
  group_by(group) %&gt;% 
  mutate(
    days_difference = ifelse(n() == 1 &amp; days_difference == 0,
                             difftime(as.Date(&#39;01-01-2020&#39;, &quot;%d-%m-%Y&quot;), as.Date(&#39;04-06-2018&#39;,&quot;%d-%m-%Y&quot;)),
                             days_difference)
  )

  group days_difference
  &lt;chr&gt;           &lt;dbl&gt;
1 a                   0
2 a                  56
3 b                 576
4 c                   0
5 c                  23

huangapple
  • 本文由 发表于 2023年4月17日 12:02:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031635.html
匿名

发表评论

匿名网友

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

确定