phil230
phil230

Reputation: 7

Spreading a single column across different columns

I am trying to take a database that looks like this:

ID      Name          Monday1             Monday2             Monday3
 1      Brad                              12:00-1:00
 2      Carly         11:00-12:00                             1:00-2:00
 3      Erin          11:00-12:00         12:00-1:00
 4      Kayla         11:00-12:00         12:00-1:00
 5      Pete                              12:00-1:00          1:00-2:00

And turn it into this:

Monday1                 Monday2                     Monday3
Carly                   Brad                        Carly
Erin                    Erin                        Pete
Kayla                   Kayla
                        Pete

I can spread the names into the columns, but there are a bunch of empty rows. I do not know how to condense the different rows. I am unsure of how to condense everything so there are not a bunch of different empty rows.

Upvotes: 0

Views: 41

Answers (1)

DatamineR
DatamineR

Reputation: 9628

After modifying your original data to have NAs instead of nothing you could try the following:

install.packages(qpcR)
do.call(qpcR:::data.frame.na, apply(df[3:5], 2, function(x) df[!is.na(x),2]))
  Monday1 Monday2 Monday3
1   Carly    Brad   Carly
2    Erin    Erin    Pete
3   Kayla   Kayla    <NA>
4    <NA>    Pete    <NA>

Upvotes: 1

Related Questions