如何添加变量并基于另一个变量的值分配值

时间:2020-03-22 03:49:20

标签: r dplyr

我想创建一个新变量(04:34:16,082 INFO [stdout] (default task-2) Hibernate: select allpotenti0_.ave_id as ave_id2_5_0_, allpotenti0_.id as id1_5_0_, allpotenti0_.id as id1_5_1_, allpotenti0_.ave_id as ave_id2_5_1_, allpotenti0_.contact_id as contact_3_5_1_, allpotenti0_.idx as idx4_5_1_, allpotenti0_.main_buyer_id as main_buy5_5_1_ from buyer allpotenti0_ where allpotenti0_.ave_id=? )并根据以下内容分配一个值 日期格式(final_session)中另一个变量的过滤值。 我已经能够添加变量 并分配一个值,然后就可以过滤和更改 变量,但我丢失了其余的观察结果(我不想这样做)。

我有以下代码:

date

以下是我的最小数据集:

## add `final_session` column, defualt value 0
old_sp_long2 <- old_sp_long %>% add_column(final_session = 0)


## select most recent date of sessions 1--15 and mark as final session == 1
df <- old_sp_long2 %>%
    filter(wave <= 15) %>%
    group_by(uci) %>%
    slice(which.max(date)) %>%
    mutate(final_session = replace(final_session, final_session == 0, 1))

我确定这是可能的,但我只是无法弄清楚。有没有人有 解决我的问题?如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

您需要这样的东西吗?

library(dplyr)

old_sp_long2 %>%
  group_by(uci) %>%
  mutate(max_date = max(date[wave <= 15], na.rm = TRUE), 
         max_wave = wave[which.max(date == max_date)],
         final_session = replace(final_session, date == max_date, 1))


#   uci     wave date       session final_session max_date   max_wave
#   <fct>  <int> <date>       <int>         <dbl> <date>        <int>
# 1 10001h     1 2016-08-29       1             0 2016-10-12        5
# 2 10001h     2 2016-09-09       2             0 2016-10-12        5
# 3 10001h     3 2016-09-16       3             0 2016-10-12        5
# 4 10001h     4 2016-09-30       4             0 2016-10-12        5
# 5 10001h     5 2016-10-12       5             1 2016-10-12        5
# 6 10001h     6 NA               6             0 2016-10-12        5
# 7 10001h     7 NA              NA             0 2016-10-12        5
# 8 10001h     8 NA              NA             0 2016-10-12        5
# 9 10001h     9 NA              NA             0 2016-10-12        5
#10 10001h    10 NA              NA             0 2016-10-12        5
# … with 41 more rows

与您的原始old_sp_long2进行观察的次数相同。