根据条件创建新列

Creating new column based on condition

我已经对数据进行了子集化,因此可以更轻松地展示我正在尝试做的事情。我正在尝试为“MaxRounds”列中的值创建一个新行的数据框。一开始MaxRounds在这样一个专栏里:

   library(dplyr);library(tidyr);library(splitstackshape)

structure(list(power = c(0.800962297001584, 0.804719517260326, 
0.808410477932415, 0.812036218849852, 0.803164810470566, 0.815597767274311
), nights = c(20L, 20L, 20L, 20L, 19L, 20L), sites = c(78L, 79L, 
80L, 81L, 81L, 82L), NonRoundedMaxRounds = c(3, 3, 3, 3, 3.15789473684211, 
3), MaxRounds = c(3, 3, 3, 3, 3, 3)), row.names = c(NA, 6L), class = "data.frame")

然后我创建了依赖于 MaxRounds 列的新行 = 根据 MaxRounds 的数量创建了重复的行。例如,如果 MaxRounds 为 2,则创建 1-2 行,如果 MaxRounds 为 5,则创建 5 行。

该代码创建一个唯一的 ID 行名称:x、x.1、x.2、x.3 等

data = expandRows(data, "MaxRounds")

structure(list(power = c(0.800962297001584, 0.800962297001584, 
0.800962297001584, 0.804719517260326, 0.804719517260326, 0.804719517260326
), nights = c(20L, 20L, 20L, 20L, 20L, 20L), sites = c(78L, 78L, 
78L, 79L, 79L, 79L), NonRoundedMaxRounds = c(3, 3, 3, 3, 3, 3
)), row.names = c("1", "1.1", "1.2", "2", "2.1", "2.2"), class = "data.frame")

然后我根据行名创建了一个新列:

data$RowID = rownames(data)

structure(list(power = c(0.800962297001584, 0.800962297001584, 
0.800962297001584, 0.804719517260326, 0.804719517260326, 0.804719517260326
), nights = c(20L, 20L, 20L, 20L, 20L, 20L), sites = c(78L, 78L, 
78L, 79L, 79L, 79L), NonRoundedMaxRounds = c(3, 3, 3, 3, 3, 3
), RowID = c("1", "1.1", "1.2", "2", "2.1", "2.2")), row.names = c("1", 
"1.1", "1.2", "2", "2.1", "2.2"), class = "data.frame")

接下来,我尝试将所有具有相同 x 值(尽管有小数点)的行组合在一起并按顺序编号。例如:

我正在尝试按“RowID”列分组,使用:

data %>% group_by(RowID) %>% mutate(id = row_number())

但是我得到这个错误:

创建独特的 Row ID 可以 by_group 完成,也可以使用 dplyr 独立完成,这里是使用 mtcars

的示例
mtcars %>% group_by(cyl) %>% mutate(
        id = row_number()
)
# Groups:   cyl [3]
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb    id
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
1  21       6   160   110  3.9   2.62  16.5     0     1     4     4     1
2  21       6   160   110  3.9   2.88  17.0     0     1     4     4     2
3  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1     1
4  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1     3
5  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2     1
6  18.1     6   225   105  2.76  3.46  20.2     1     0     3     1     4

没有grouping,

mtcars %>% mutate(
        id = row_number()
) 
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb id
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4  1
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4  2
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1  3
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1  4
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2  5
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1  6

row_number() 按组或不按顺序对每一行进行编号。例如,grouped 示例中的第 4 行具有 id=3,因为它是 6 cyl(inders)group 中的第 3 行。