[英]How to add a “group” column in R based on the row number
I have a large df which has count data generated from two different programs.我有一个很大的 df,其中包含从两个不同程序生成的计数数据。 This is an example of the df:
这是df的一个例子:
Species variable value
1 "Malassezia;globosa" 100 68126
2 "Aspergillus;nomius" 100 13977
3 "Mitosporidium;daphniae" 100 5953
4 "Penicillium;chrysogenum" 100 1
5 Other 100 102
6 "Malassezia;globosa" 101 110268
In total there are 311 rows.总共有 311 行。 I want to add another column titled "Program" which groups rows 1 to 186 as "HMS" and rows 187 to 311 as "MiCoP", for example:
我想添加另一个标题为“程序”的列,它将第 1 到 186 行分组为“HMS”,将第 187 到 311 行分组为“MiCoP”,例如:
Species variable value Program
1 "Malassezia;globosa" 100 68126 HMS
2 "Aspergillus;nomius" 100 13977 HMS
3 "Mitosporidium;daphniae" 100 5953 HMS
4 "Penicillium;chrysogenum" 100 1 HMS
5 Other 100 102 HMS
6 "Malassezia;globosa" 101 110268 HMS
If you want to assign groups based on row number, you can do:如果要根据行号分配组,可以执行以下操作:
df$Program <- NA #initialise
df$Program[1:186] <- "HMS"
df$Program[187:311] <- "MiCoP"
We can do this with case_when
我们可以用
case_when
做到这一点
library(dplyr)
df <- df %>%
mutate(Program = case_when(row_number() < 187 ~ "HMS",
between(row_number(), 187, 311) ~ "MiCoP"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.