英文:
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('01-01-2020', "%d-%m-%Y"), as.Date('04-06-2018',"%d-%m-%Y"))
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 %>%
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论