0%

R dplyr's spread and gather

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# import datase as dataframe
hang<-read.csv('score.csv', header=TRUE)
lie<-read.csv('TMP_SCORE.csv', header=TRUE)

# dplyr gather and spread
library(dplyr)
library(tidyr)
## spread hang to lie
head(hang)
spreaded<-hang %>%
spread(c_name, grade) %>%
replace(is.na(.), 0) %>%
group_by(stu_id) %>%
summarise(across(everything(), sum)) %>%
select(-id)

head(spreaded)

## gather lie to hang
head(lie)
gathered <- lie %>%
gather("c_name", "grade", -1) %>%
filter(grade!=0) %>%
arrange(stu_id)
head(gathered)