[英]R Using case_when to track changes in a column by group
I have a dataset of course enrollment where I am trying to track whether students dropped, added, or retained a course throughout the semester and identify their enrollment 'path'.我有一个课程注册数据集,我试图在其中跟踪学生在整个学期中是否放弃、添加或保留一门课程,并确定他们的注册“路径”。 Ie I want to record if they were enrolled in BIOL101 and dropped it to take BIOL202.
即我想记录他们是否注册了 BIOL101 并放弃了它以参加 BIOL202。 My dataframe looks like this:
我的数据框如下所示:
YRTR TECH_ID COU_ID SUBJ COU_NBR GENDER RACE sub_cou status path
20173 108 217 MUSC 2231 Male White MUSC 2231 retained
20173 108 218 MUSC 2281 Male White MUSC 2281 retained
20173 8429 574 ECON 2201 Male White ECON 2201 retained
20173 8429 720 BUSN 2120 Male White BUSN 2120 retained
20173 9883 60 ECON 2202 Male White ECON 2202 added
20173 15515 95 PHIL 1102 Female White PHIL 1102 retained
20183 8207 478 ART 1102 Female White ART 1102 retained
20183 8207 1306 ART 1130 Female White ART 1130 added
20183 8207 403 ART 1125 Female White ART 1125 dropped
I am trying to fill in the column on the far right, "path".我正在尝试填写最右侧的“路径”列。 The idea is that if a student is retained in a course like in the first row, the path would read
2231->2231
.这个想法是,如果学生被保留在第一行的课程中,则路径将显示为
2231->2231
。 Specifically I am looking at course transfers WITHIN subjects.
具体来说,我期待在WITHIN学科课程转移。 So, at the end of the data set, ID 8207 would have one path that looked like
1102->1102
and another path that looked like 1125->1130
因此,在数据集的末尾,ID 8207 将有一条看起来像
1102->1102
路径和另一条看起来像1125->1130
路径
I initially tried splitting the dataframe into two dataframes (one before, and one after the drop period) and then rejoining them like so:我最初尝试将数据帧拆分为两个数据帧(一个在丢弃期之前,一个在丢弃期之后),然后像这样重新加入它们:
data5 <- merge(x=post_drop, y=pre_drop, by=c("TECH_ID", "YRTR", "SUBJ"), all=TRUE)
And then using case_when to assign the path:然后使用 case_when 分配路径:
data5$status.x=="retained" ~ paste0(data5$COU_NBR.x, "->", data5$COU_NBR.x),
((data5$status.x=="added") & (data5$status.y=="dropped")) ~ paste0(data5$COU_NBR.y, "->", data5$COU_NBR.x),
((data5$status.x=="dropped") & (data5$status.y=="added")) ~ paste0(data5$COU_NBR.x, "->", data5$COU_NBR.y)
)
But this doesn't get me where I want - it leaves a lot of NAs in paths and also doesn't tell me if a student dropped a course within a subject and didn't register for another (ie dropping BIOL101 and not taking another BIOL class) in which case I would want something like 101->NA
or when a class is simply added (ie they weren't registered in a BIOL class initially but decided to register for BIOL101) which would be formatted like so NA->101
但这并没有让我到达我想要的地方 - 它在路径中留下了很多 NA,并且也没有告诉我学生是否放弃了一个学科的课程并且没有注册另一个(即放弃 BIOL101 而不是参加另一个BIOL 类)在这种情况下,我想要像
101->NA
这样的东西,或者当一个类被简单地添加时(即他们最初没有在 BIOL 类中注册,但决定注册 BIOL101),其格式将像这样NA->101
EDITED SOLUTION Spetember 27th已编辑解决方案 2 月 27 日
Hello again @alexvc Here's a start.你好@alexvc 这是一个开始。 Knowing a little bit about your data.
对你的数据有一点了解。 You forget the case where a student dropped 1 and added 2 in which case the "path" becomes muddled.
您忘记了学生删除 1 并添加 2 的情况,在这种情况下,“路径”变得混乱。 I've given you a solution that shows
path
clearly.我给了你一个清晰显示
path
的解决方案。
library(dplyr)
library(tidyr)
data5 %>%
group_by(YRTR, TECH_ID, SUBJ, status) %>%
mutate(numbadd =
case_when(
status == "added" ~ 1,
TRUE ~ 0
),
numbdrop =
case_when(
status == "dropped" ~ 1,
TRUE ~ 0
),
rightside =
case_when(
numbadd == 1 ~ paste(COU_NBR, collapse = " and ")
),
leftside =
case_when(
numbdrop == 1 ~ paste(COU_NBR, collapse = " and ")
)
) %>%
group_by(YRTR, TECH_ID, SUBJ) %>%
mutate(total_add_drop = ifelse(status == "retained",
0,
sum(numbadd) + sum(numbdrop))) %>%
tidyr::fill(leftside, rightside, .direction = "downup") %>%
group_by(YRTR, TECH_ID, SUBJ, status) %>%
mutate(PATH =
case_when(
status == "retained" ~ paste(COU_NBR,
COU_NBR,
sep = " -> "),
status == "added" & total_add_drop == 1 ~ paste("NA",
COU_NBR,
sep = " -> "),
status == "dropped" & total_add_drop == 1 ~ paste(COU_NBR,
"NA",
sep = " -> "),
total_add_drop >= 2 ~ paste(leftside,
rightside,
sep = " -> "),
TRUE ~ "Theres a problem"
)) %>%
arrange(YRTR, TECH_ID) %>%
select(-COU_ID, -GENDER, -RACE, -rightside, -leftside, -numbadd, -numbdrop, -total_add_drop)
#> # A tibble: 17 x 7
#> # Groups: YRTR, TECH_ID, SUBJ, status [13]
#> YRTR TECH_ID SUBJ COU_NBR sub_cou status PATH
#> <dbl> <dbl> <chr> <dbl> <chr> <chr> <chr>
#> 1 20173 108 MUSC 2231 MUSC 2231 retained 2231 -> 2231
#> 2 20173 108 MUSC 2281 MUSC 2281 retained 2281 -> 2281
#> 3 20173 3889 ECON 2202 ECON 2202 dropped 2202 -> NA
#> 4 20173 8429 ECON 2201 ECON 2201 retained 2201 -> 2201
#> 5 20173 8429 BUSN 2120 BUSN 2120 retained 2120 -> 2120
#> 6 20173 9883 ECON 2202 ECON 2202 added NA -> 2202
#> 7 20173 15515 PHIL 1102 PHIL 1102 retained 1102 -> 1102
#> 8 20183 8207 ART 1102 ART 1102 retained 1102 -> 1102
#> 9 20183 8207 ART 1130 ART 1130 added 1125 -> 1130 and 2345
#> 10 20183 8207 ART 2345 ART 2345 added 1125 -> 1130 and 2345
#> 11 20183 8207 ART 1125 ART 1125 dropped 1125 -> 1130 and 2345
#> 12 20183 8209 ART 2345 ART 2345 added 1125 -> 2345
#> 13 20183 8209 ART 1125 ART 1125 dropped 1125 -> 2345
#> 14 20183 8270 PSYC 1001 PSYC 1001 dropped 1001 and 1002 -> 1003 and 1004
#> 15 20183 8270 PSYC 1003 PSYC 1003 added 1001 and 1002 -> 1003 and 1004
#> 16 20183 8270 PSYC 1002 PSYC 1002 dropped 1001 and 1002 -> 1003 and 1004
#> 17 20183 8270 PSYC 1004 PSYC 1004 added 1001 and 1002 -> 1003 and 1004
Your data with additional test cases您的数据与其他测试用例
data5 <- readr::read_table(
" YRTR TECH_ID COU_ID SUBJ COU_NBR GENDER RACE sub_cou status
20173 108 217 MUSC 2231 Male White MUSC 2231 retained
20173 108 218 MUSC 2281 Male White MUSC 2281 retained
20173 8429 574 ECON 2201 Male White ECON 2201 retained
20173 8429 720 BUSN 2120 Male White BUSN 2120 retained
20173 9883 60 ECON 2202 Male White ECON 2202 added
20173 3889 60 ECON 2202 Male White ECON 2202 dropped
20173 15515 95 PHIL 1102 Female White PHIL 1102 retained
20183 8207 478 ART 1102 Female White ART 1102 retained
20183 8207 1306 ART 1130 Female White ART 1130 added
20183 8207 1307 ART 2345 Female White ART 2345 added
20183 8207 403 ART 1125 Female White ART 1125 dropped
20183 8270 1306 PSYC 1001 Female Black PSYC 1001 dropped
20183 8270 1307 PSYC 1003 Female Black PSYC 1003 added
20183 8270 403 PSYC 1002 Female Black PSYC 1002 dropped
20183 8209 1307 ART 2345 Female White ART 2345 added
20183 8270 1306 PSYC 1004 Female Black PSYC 1004 added
20183 8209 403 ART 1125 Female White ART 1125 dropped")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.