Мы можем преобразовать в «длинный» формат, получить элементы distinct
, создать столбец из 1 с, а затем преобразовать обратно в «широкий» формат в tidyverse
library(dplyr)
library(tidyr) #1.0.0
df1 %>%
pivot_longer(cols = -StudentName, values_drop_na = TRUE) %>%
distinct(StudentName, value) %>%
mutate(n = 1) %>%
pivot_wider(names_from = value, values_from= n, values_fill = list(n = 0))
# A tibble: 6 x 18
# StudentName ITSF4090 ITSF5008 ITSF5035 HUDK4050 QMSS5010 QMSS5015 QMSS5072 STAT4205 QMSS5021 ITSF4025 HUDK4029 HUDK4052 CCPJ5062 `A&HA4063`
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Student1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
#2 Student2 0 0 0 1 1 1 1 1 1 0 0 0 0 0
#3 Student3 1 0 1 1 0 0 0 0 0 1 0 0 0 0
#4 Student4 0 0 0 1 0 0 0 0 0 0 1 1 1 1
#5 Student5 0 0 0 1 0 0 0 0 0 0 1 1 0 0
#6 Student6 0 0 0 1 0 0 0 0 0 0 0 0 0 0
# … with 3 more variables: HUDM4125 <dbl>, HUDM5026 <dbl>, HUDM5126 <dbl>
data
df1 <- structure(list(StudentName = c("Student1", "Student2", "Student3",
"Student4", "Student5", "Student6"), Class1 = c("ITSF4090", "HUDK4050",
"HUDK4050", "HUDK4050", "HUDK4050", "HUDK4050"), Class2 = c("ITSF5008",
"QMSS5010", "ITSF4090", "HUDK4029", "HUDK4052", "HUDM4125"),
Class3 = c("ITSF5035", "QMSS5015", "ITSF4025", "HUDK4052",
"HUDK4029", "HUDM5026"), Class4 = c("HUDK4050", "QMSS5072",
"ITSF5035", "CCPJ5062", NA, "HUDM5126"), Class5 = c(NA, "STAT4205",
NA, "A&HA4063", NA, NA), Class6 = c(NA, "QMSS5021", NA, NA,
NA, NA)), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6"))