[英]Assign value in column by group R
I have data that looks like this:我有看起来像这样的数据:
data <- data.frame(stringsAsFactors=FALSE,
id = c(1, 1, 2, 2, 3, 3, 3, 4),
rating = c("No rating", "Red", "No rating", "Red", "Green", "Red",
"No rating", "Green"),
pct = c(10.34079909, 89.65920091, 91.28335721, 8.71664279, 21, 83, 2,
10)
I am trying to create a new variable, called flag
, to determine when a group, id
meets a certain condition.我正在尝试创建一个名为
flag
的新变量,以确定组id
何时满足特定条件。 For example:例如:
data %>%
group_by(id) %>%
mutate(flag = case_when(
pct > .05 & rating == "Red" ~ TRUE,
TRUE ~ FALSE))
Once the flag condition is met, I want all values in flag
to be TRUE
for that specific id
, not only for the rows where that condition is met.一旦满足标志条件,我希望
flag
所有值对于该特定id
为TRUE
,而不仅仅是对于满足该条件的行。
I am not to familiar with dplyr, but this does work if I understand the question correctly我不熟悉 dplyr,但如果我正确理解问题,这确实有效
data <- data.frame(stringsAsFactors=FALSE, id = c(1, 1, 2, 2, 3, 3, 3, 4), rating = c("No rating", "Red", "No rating", "Red", "Green", "Red", "No rating", "Green"), pct = c(10.34079909, 89.65920091, 91.28335721, 8.71664279, 21, 83, 2, 10))
data$bin <- ifelse(data$pct > .05 & data$rating == "Red", TRUE, FALSE)
df <- merge(data, aggregate(data=data, bin~id, max), by.x="id", by.y = "id")
head(df)
id rating pct bin.x bin.y
1 1 No rating 10.340799 FALSE 1
2 1 Red 89.659201 TRUE 1
3 2 No rating 91.283357 FALSE 1
4 2 Red 8.716643 TRUE 1
5 3 Green 21.000000 FALSE 1
6 3 Red 83.000000 TRUE 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.