2

I am trying to spread a single column in an R dataframe. I have reviewed many posts on SO, but cant get my solution to work because most solutions seem to require a formula (count, mean, sum, etc). I am simply looking to spread a column of characters. For example:

library(tidyverse)

school<- c("univ1", "univ2","univ1", "univ2","univ1" )
student<-c('bob', 'sally','ben','trudy','evan')

df <- data.frame(school, student)

produces:

school            student
univ1             bob
univ2             sally
univ1             ben
univ2             trudy
univ1             evan

but what I am trying to output is:

school            student1      student2     student2
univ1             bob           ben          evan
univ2             sally         trudy

How would I accomplish this? I tried spread() and pivot_wider() but neither work. Any thoughts? The actual dataset is quite large (over 300k rows of data) that will need transposed in this manner if that makes a difference.

cowboy
  • 613
  • 5
  • 20

2 Answers2

3

You need to specify the student1, student2 and student3 before you use spread(). I'd suggest adding a new column to spread by, for example:

df %>%
group_by(school) %>%
mutate(
 student_number = row_number(),
 student_number = str_c("student_", student_number)
) %>%
ungroup() %>%
spread(student_number,student)
BeckyPat
  • 86
  • 5
3

For each group you specify the student number and spread according to that

df %>% group_by(school) %>% mutate(n=paste("student",1:n())) %>% spread(n,student)
StupidWolf
  • 45,075
  • 17
  • 40
  • 72