我发现了这个: Remove subsequent rows of a group after first occurence of 0 in a column个
还有这个: Remove rows after first occurrence of a certain value个
但这并不完全是我想要的.下面是一段重述:
df <- data.frame(
id = c(1001, 1001, 1002, 1002, 1002, 1005, 1005, 1005, 1005, 1005),
name = c("monkey", "gorilla", "chimp", "monkey", "giraffe", "tarzan", "whale", "princess", "phone", "kindle"),
char = c(0, 1, 0, 1, 0, 0, 0, 1, 0, 0))
df
#> id name char
#> 1 1001 monkey 0
#> 2 1001 gorilla 1
#> 3 1002 chimp 0
#> 4 1002 monkey 1
#> 5 1002 giraffe 0
#> 6 1005 tarzan 0
#> 7 1005 whale 0
#> 8 1005 princess 1
#> 9 1005 phone 0
#> 10 1005 kindle 0
df_desired <- data.frame(
id = c(1001, 1002, 1005, 1005),
name = c("monkey", "chimp", "tarzan", "whale"),
char = c(0, 0, 0, 0))
df_desired
#> id name char
#> 1 1001 monkey 0
#> 3 1002 chimp 0
#> 6 1005 tarzan 0
#> 7 1005 whale 0
由reprex package(v2.0.1)于2022-08-10创建
当按id分组并按名称排列时,我正在try 删除char达到1之后的行及其后续行.