Для этого можно использовать функцию dcast
из пакета data.table
...
# install.packages('data.table')
library(data.table)
# Coerce the data.frame to a data.table.
dt <- data.table(df)
# Cast the data.table ...
dcast(dt, StudentID + City ~ StudentStatus)
# StudentID City Dropped Out Enrolled Graduated
# 1: 1 Long Beach, CA 12/21/2012 8/21/2011 <NA>
# 2: 2 Long Beach, CA <NA> 8/21/2011 12/11/2014
# 3: 3 Long Beach, CA <NA> 11/21/2011 1/21/2016
Пример данных:
df <- data.frame(StudentID = c(1, 2, 3, 1, 2, 3),
City = rep("Long Beach, CA", 6),
StudentStatus = c("Enrolled", "Enrolled","Enrolled", "Dropped Out", "Graduated", "Graduated"),
StatusDate = c("8/21/2011","8/21/2011","11/21/2011","12/21/2012","12/11/2014","1/21/2016"))