如何更改组中的值?

时间:2019-03-14 01:50:26

标签: r dataframe dplyr

我创建了一个嵌套成功的列,如果嵌套的命运是“收获”或“成熟”,则值为“ 1”,如果嵌套的命运是“嵌套失败”,则为0。在某些情况下,巢穴的命运在第一次访问中“增加”,而在第二次访问中“失败”。在这种情况下,单个嵌套的成功结果同时为1和0(请参见嵌套“ D063”和“ D063”)。

如何在同一嵌套成功的情况下删除“ 1”或分配“ NA”,并且仅在同时具有1和0的情况下保留“ 0”?

换句话说,我希望每个嵌套只有一个成功结果(单个1或0),而不是多个。而且,我想保留所有行。

My data looks like this:

示例数据:

structure(list(date = structure(c(4L, 2L, 1L, 5L, 3L, 1L, 5L, 
2L, 1L, 5L, 3L, 1L, 5L, 2L, 1L), .Label = c("14/06/2018", "17/05/2018", 
"21/05/2018", "5/05/2018", "6/05/2018"), class = "factor"), nest.code = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("D046", 
"D047", "D062", "D063", "W18003"), class = "factor"), year = c(2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L), species = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("AA", 
"BB"), class = "factor"), visit = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), eggs = c(1L, 0L, 0L, 1L, 0L, 
0L, 2L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L), chicks = c(0L, NA, NA, 
0L, 1L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, NA, 1L), fate = structure(c(2L, 
4L, 5L, 2L, 4L, 3L, 2L, 4L, 3L, 2L, 4L, 3L, 2L, 5L, 1L), .Label = c("fledged", 
"incubating", "nest failed", "rearing", "unknown"), class = "factor"), 
    success = c(NA, 1L, NA, NA, 1L, 0L, NA, 1L, 0L, NA, 1L, 0L, 
    NA, NA, 1L)), class = "data.frame", row.names = c(NA, -15L
))

这是我尝试的代码:

datanew <- data %>% 
  group_by(year, species, nest.code)%>% 
  mutate(Real_success = ifelse(success ==1 & 0, 0, success))

2 个答案:

答案 0 :(得分:0)

我不确定您最终的想法。是否要保留所有行,是否要以某种方式对其进行排序。无论如何,这就是我想出的:

更新:对不起,我错过了第一个答案中的“成熟”


dat %>% 
  group_by(year, species, nest.code)%>%
  arrange(year, species, nest.code, success) %>%
  mutate(success = ifelse(row_number() > 1, NA, success))

# A tibble: 15 x 9
# Groups:   year, species, nest.code [5]
   date       nest.code  year species visit  eggs chicks outcome     success
   <fct>      <fct>     <int> <fct>   <int> <int>  <int> <fct>         <int>
 1 17/05/2018 D046       2018 AA          2     0     NA rearing           1
 2 5/05/2018  D046       2018 AA          1     1      0 incubating       NA
 3 14/06/2018 D046       2018 AA          3     0     NA unknown          NA
 4 14/06/2018 D047       2018 AA          3     0      0 nest failed       0
 5 21/05/2018 D047       2018 AA          2     0      1 rearing          NA
 6 6/05/2018  D047       2018 AA          1     1      0 incubating       NA
 7 14/06/2018 D062       2018 AA          3     0      0 nest failed       0
 8 17/05/2018 D062       2018 AA          2     0      2 rearing          NA
 9 6/05/2018  D062       2018 AA          1     2      0 incubating       NA
10 14/06/2018 D063       2018 AA          3     0      0 nest failed       0
11 21/05/2018 D063       2018 AA          2     0      1 rearing          NA
12 6/05/2018  D063       2018 AA          1     1      0 incubating       NA
13 14/06/2018 W18003     2018 BB          3     0      1 fledged           1
14 6/05/2018  W18003     2018 BB          1     1      0 incubating       NA
15 17/05/2018 W18003     2018 BB          2     0     NA unknown          NA

肯定会有一些简单的方法可以做到这一点。我自己在dplyr中没有亲。 如果可行,我很高兴。

答案 1 :(得分:0)

这是一种方法,该方法将至少有一个失败的嵌套的所有行都置于零,如果至少有一个成功且没有失败,则为1,否则为NA:

library(dplyr)
mydata %>% 
  group_by(year, species, nest.code) %>% 
  mutate(real_success = case_when(
    sum(1-success, na.rm = T) > 0   ~   0, # There was a fail
    sum(success,   na.rm = T) > 0   ~   1,
    TRUE                            ~   NA_real_)) %>%
 ungroup()

# A tibble: 15 x 10
   date       nest.code  year species visit  eggs chicks fate       success real_success
   <fct>      <fct>     <int> <fct>   <int> <int>  <int> <fct>        <int>        <dbl>
 1 5/05/2018  D046       2018 AA          1     1      0 incubating      NA            1
 2 17/05/2018 D046       2018 AA          2     0     NA rearing          1            1
 3 14/06/2018 D046       2018 AA          3     0     NA unknown         NA            1
 4 6/05/2018  D047       2018 AA          1     1      0 incubating      NA            0
 5 21/05/2018 D047       2018 AA          2     0      1 rearing          1            0
 6 14/06/2018 D047       2018 AA          3     0      0 nest fail…       0            0
 7 6/05/2018  D062       2018 AA          1     2      0 incubating      NA            0
 8 17/05/2018 D062       2018 AA          2     0      2 rearing          1            0
 9 14/06/2018 D062       2018 AA          3     0      0 nest fail…       0            0
10 6/05/2018  D063       2018 AA          1     1      0 incubating      NA            0
11 21/05/2018 D063       2018 AA          2     0      1 rearing          1            0
12 14/06/2018 D063       2018 AA          3     0      0 nest fail…       0            0
13 6/05/2018  W18003     2018 BB          1     1      0 incubating      NA            1
14 17/05/2018 W18003     2018 BB          2     0     NA unknown         NA            1
15 14/06/2018 W18003     2018 BB          3     0      1 fledged          1            1