create variable conditionally by group in R (write function)

依然范特西╮ 提交于 2019-12-11 15:09:15

问题


I want to create a variable by group conditioned on existing variable on individual level. Each individual has a outlier variable 1, 2, 3. I want to create a new variable by group so that the new var = 2 whenever there is at least one individual in that group whose outlier variable = 2; and the new var = 3 whenever there is at least one individual in that group whose outlier variable = 3.

The data looks like this

grpid id outlier
1     1   1
1     2   1
1     3   2
2     4   1
2     5   3
2     6   1
3     7   1
3     8   1
3     9   1

Ideal output like this

grpid id outlier  goutlier
1     1   1       2
1     2   1       2
1     3   2       2
2     4   1       3
2     5   3       3
2     6   1       3
3     7   1       1
3     8   1       1
3     9   1       1

Any suggestions?

Thanks!


回答1:


It is easy with dplyr

library(dplyr)
df <- read.table(header = TRUE,sep = ",",
                 text = "grpid,id,outlier
1,1,1
1,2,1
1,3,2
2,4,1
2,5,3
2,6,1
3,7,1
3,8,1
3,9,1")

df %>% group_by(grpid) %>% mutate(goutlier = max(outlier))


来源:https://stackoverflow.com/questions/47641901/create-variable-conditionally-by-group-in-r-write-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!