flag the record with repeat within a group using R
To flag the record with repeat within a group using R, you can use the dplyr package. Here's an example:
library(dplyr)
# create sample data
df <- data.frame(
group = c(1, 1, 1, 2, 2, 3),
value = c(5, 7, 5, 3, 2, 4)
)
# group by 'group' and add a new column 'flag' that indicates if 'value' is repeated within the group
df <- df %>%
group_by(group) %>%
mutate(
flag = ifelse(duplicated(value) | duplicated(value, fromLast = TRUE), "repeat", "unique")
)
# view the result
df
This will add a new column called 'flag' to the original data frame, which will indicate if the 'value' is repeated within each 'group'. If the 'value' is repeated, the flag will be set to "repeat", otherwise it will be set to "unique".
原文地址: https://www.cveoy.top/t/topic/bUfa 著作权归作者所有。请勿转载和采集!