I have a data frame that I want to reshape so that for each medid, there is only one row. I know that I can use spread to accomplish this, but it's not looking at all how I want to look and I just can't wrap my head around the concept when I read blogs, tutorials, etc. and I'm just not sure where I'm going amiss.
My data currently looks like
medid year dose drug
1 101 2001 500 A
2 101 2002 600 A
3 101 2003 750 A
4 101 2004 550 A
5 102 2001 300 B
6 102 2002 330 B
7 102 2003 350 B
8 102 2004 390 B
9 103 2001 100 C
10 103 2002 NA C
11 103 2003 250 C
12 103 2004 125 C
but I want it to look like
medid dose.2001 dose.2002 dose.2003 dose.2004 drug.2001 drug.2002 drug.2003 drug.2004
1 101 500 600 750 550 A A A A
2 102 300 330 350 390 B B B B
3 103 100 NA 250 125 C C C C
I've tried to use spread()
but it doesn't give me unique row for each GEOID, and it just isn't looking how I want.
df <- data.frame(medid=c(101, 101, 101, 101, 102, 102, 102, 102, 103, 103, 103, 103),
year=c(2001, 2002, 2003, 2004, 2001, 2002, 2003, 2004, 2001, 2002, 2003, 2004),
dose=c(500, 600, 750, 550, 300, 330, 350, 390, 100, NA, 250, 125),
drug=c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C"))
df %>% spread(year, medid)