I've been trying to figure out how the melt and cast functions works with the reshape2 package. But can't get the results that I'm looking for.
Heres the data:
data <- read.table(header=T, text="
diagnosis agrp events Period
COPD 1 16 1998-1999
COPD 2 51 1998-1999
COPD 3 27 1998-1999
COPD 4 9 1998-1999
COPD 1 44 2000-2001
COPD 2 122 2000-2001
COPD 3 39 2000-2001
COPD 4 12 2000-2001")
Heres what im trying to acheive
diagnosis agrp 1998-1999 2000-2001 etc...
COPD 1 16 44
COPD 2 51 12
COPD 3 27 39
COPD 4 9 12
I'd like to transpose the data so that "Period" becomes a column for itself. Sufficient code to achieve this would be highly appreciated!
Update
This is how my data looks like:
data <- read.table(header=T, text="
diagnosis agrp 1998-1999 2000-2001
KONTROLL 1 140903 72208
KONTROLL 2 88322 33704
KONTROLL 3 18175 3804
KONTROLL 4 6125 797")
This is what I'm trying to achieve:
diagnosis agrp 1998-1999 2000-2001 Total
KONTROLL 1 140903 72208 213111
KONTROLL 2 88322 33704 122026
KONTROLL 3 18175 3804 21979
KONTROLL 4 6125 797 6922