R语言 如果else基于几个条件和仅特定的行位置?

xqnpmsa8  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(80)

我试图根据一组条件将变量变异为0或1。然而,一旦满足了条件,我实际上只需要前几行(让我们基于小例子说第一行),而不是改变所有行。我正在努力在我的ifelse语句中索引这些头寸。
我的dataframe看起来像这样:

df <-data.frame(individual = c("ind1", "ind1", "ind1", "ind1", "ind1", 
                               "ind1", "ind1", "ind1", "ind1",
                               "ind1", "ind1", "ind1", "ind1", "ind1",
                               "ind1", "ind1", "ind1",
                               "ind2", "ind2", "ind2", "ind2", "ind2", 
                               "ind2", "ind2", "ind2", "ind2",
                               "ind2", "ind2", "ind2", "ind2", "ind2",
                               "ind2", "ind2", "ind2"),
                uniqueclass = c("migrate1", "migrate1", 
                                "stop1", "stop1", "stop1",
                                "migrate2", "migrate2", "migrate2", "migrate2",
                                "stop2", "stop2", 
                                "migrate3", "migrate3", "migrate3",
                                "winter1", "winter1", "winter1",
                                "migrate1", "migrate1", 
                                "stop1", "stop1", "stop1",
                                "migrate2", "migrate2", "migrate2", "migrate2",
                                "stop2", "stop2", 
                                "migrate3", "migrate3", "migrate3",
                                "winter1", "winter1", "winter1"),
                class = c("migrate", "migrate", "stop", "stop", "stop", 
                          "migrate", "migrate", "migrate", "migrate",
                          "stop", "stop", "migrate", "migrate", "migrate",
                          "winter", "winter", "winter",
                          "migrate", "migrate", "stop", "stop", "stop", 
                          "migrate", "migrate", "migrate", "migrate",
                          "stop", "stop", "migrate", "migrate", "migrate",
                          "winter", "winter", "winter"),
                mig_dur = c(10, 10, NA, NA, NA, 
                            2, 2, 2, 2,
                            NA, NA, 4, 4, 4,
                            NA, NA, NA,
                            6, 6, NA, NA, NA, 
                            3, 3, 3, 3,
                            NA, NA, 1, 1, 1,
                            NA, NA, NA)
)

字符串
我想做的是基于class == "migrate"创建一个新列response,其中migrate = 1,否则为0,* 但 * 仅选择前几行(在本例中,仅选择第一行)以基于唯一事件uniqueclass变量创建1,其中只有每个唯一事件的第一个migrate行变为1,其他所有行均变为0。
我的代码看起来像这样--我在ifelse语句中的位置索引方面遇到了问题:

df_new <- df %>% group_by(individual) %>%
  mutate(response = ifelse(!is.na(mig_dur) & uniqueclass[1,], 1, 0)))


同样,我想要的输出是将uniqueclass == migratex的第一行(即x = 1.n)变为1,而所有其他(包括其他迁移位置)以及所有stop和winter类变为0。
任何建议将不胜感激。尼克

ovfsdjhp

ovfsdjhp1#

我想你想要:

library(dplyr)

df %>%
  mutate(response = as.integer(class == "migrate" & row_number() <= 1), .by = c(individual, uniqueclass))

字符串
您可以通过更改row_number <= n来调整标记的行数。
提供:

individual uniqueclass   class mig_dur response
1        ind1    migrate1 migrate      10        1
2        ind1    migrate1 migrate      10        0
3        ind1       stop1    stop      NA        0
4        ind1       stop1    stop      NA        0
5        ind1       stop1    stop      NA        0
6        ind1    migrate2 migrate       2        1
7        ind1    migrate2 migrate       2        0
8        ind1    migrate2 migrate       2        0
9        ind1    migrate2 migrate       2        0
10       ind1       stop2    stop      NA        0
11       ind1       stop2    stop      NA        0
12       ind1    migrate3 migrate       4        1
13       ind1    migrate3 migrate       4        0
14       ind1    migrate3 migrate       4        0
15       ind1     winter1  winter      NA        0
16       ind1     winter1  winter      NA        0
17       ind1     winter1  winter      NA        0
18       ind2    migrate1 migrate       6        1
19       ind2    migrate1 migrate       6        0
20       ind2       stop1    stop      NA        0
21       ind2       stop1    stop      NA        0
22       ind2       stop1    stop      NA        0
23       ind2    migrate2 migrate       3        1
24       ind2    migrate2 migrate       3        0
25       ind2    migrate2 migrate       3        0
26       ind2    migrate2 migrate       3        0
27       ind2       stop2    stop      NA        0
28       ind2       stop2    stop      NA        0
29       ind2    migrate3 migrate       1        1
30       ind2    migrate3 migrate       1        0
31       ind2    migrate3 migrate       1        0
32       ind2     winter1  winter      NA        0
33       ind2     winter1  winter      NA        0
34       ind2     winter1  winter      NA        0

相关问题