R:生成指标,组内的值不同

时间:2017-11-01 22:48:39

标签: r

我有一个数据框,每行都是一个观察点,我有两列:

  • 观察的group成员资格
  • 观察outcome

我尝试创建一个新变量outcome_change,如果outcome与给定group中的所有观察值不相同,则值为1,否则为0。 / p>

以下代码(dat)中显示的是我拥有的数据示例。同时,dat_out1显示我在没有NA值的情况下查找要生成的代码。 dat_out2是相同的,只是它表示当组值中缺少值时会出现相同的结果。

肯定有一些与dplyr :: group_by()有关吗?我不知道如何在小组内进行这些比较。

  # Input (2 groups: 1 with identical values of outcome
  # in the group (group a) and 1 with differing values of
  # outcome in the group (group b) 
  dat <- data.frame(group = c("a","a","a","b","b","b"), 
                    outcome = c(1,1,1,3,2,2))

  # Output 1: add a variable for all observations belonging to
  # a group where the outcome changed within each group
  dat_out1 <-   data.frame(group = c("a","a","a","b","b","b"), 
                                  outcome = c(1,1,1,3,2,2),
                                  outcome_change = c(0,0,0,1,1,1))

  # Output 2: same as Output 1, but able to ignore NA values
  dat_out2 <-     data.frame(group = c("a","a","a","b","b","b"), 
                              outcome = c(1,1,NA,3,2,NA),
                              outcome_change = c(0,0,0,1,1,1))

3 个答案:

答案 0 :(得分:3)

这是一个方法:

library(tidyverse)
dat %>%
  group_by(group) %>%
  mutate(outcome_change = ifelse(length(unique(outcome[!is.na(outcome)])) > 1, 1, 0))
#output
# A tibble: 6 x 3
# Groups:   group [2]
   group outcome outcome_change
  <fctr>   <dbl>          <dbl>
1      a       1              0
2      a       1              0
3      a       1              0
4      b       3              1
5      b       2              1
6      b       2              1

dat2

# A tibble: 6 x 3
# Groups:   group [2]
   group outcome outcome_change
  <fctr>   <dbl>          <dbl>
1      a       1              0
2      a       1              0
3      a      NA              0
4      b       3              1
5      b       2              1
6      b      NA              1

答案 1 :(得分:1)

library(dplyr)
dat <- data.frame(group = c("a","a","a","b","b","b"), 
                  outcome = c(1,1,1,3,2,2))
dat2 <- data.frame(group = c("a","a","a","b","b","b"), 
                   outcome = c(1,1,NA,3,2,NA))

dat_out1 <- dat %>% group_by(group) %>% 
  mutate(outcome_change = ifelse(min(outcome) == max(outcome), 0, 1))

dat_out2 <- dat2 %>% group_by(group) %>% 
  mutate(outcome_change = ifelse(min(outcome, na.rm = TRUE) == max(outcome, na.rm = TRUE), 0, 1))

答案 2 :(得分:1)

以下是使用data.table

的选项
library(data.table)
setDT(dat1)[, outcome_change := as.integer(uniqueN(outcome[!is.na(outcome)])>1), group]
dat1
#   group outcome outcome_change
#1:     a       1              0  
#2:     a       1              0
#3:     a       1              0
#4:     b       3              1
#5:     b       2              1
#6:     b       2              1

如果我们将相同的内容应用于'dat2'

dat2
#    group outcome outcome_change2
#1:     a       1               0
#2:     a       1               0
#3:     a      NA               0
#4:     b       3               1
#5:     b       2               1
#6:     b      NA               1