Christopher W. Ryan
2008-Oct-30 20:58 UTC
[R] trying to figure out reorder.factor in gdata
I'm trying to make a factor with levels for day of week, appear as Monday-Sunday, rather than alphabetically. I'm using reorder.factor from gdata package, but obviously not using it properly. What am I doing wrong? Thanks.>data <- read.table("DataEarly2008.txt", sep=" ", header=T, as.is=1:2) >str(data)'data.frame': 7953 obs. of 9 variables: $ AdmitDate : chr "6/28/2004" "9/24/2004" "12/29/2004" "3/15/2005" ... $ DischargeDate : chr "6/24/2008" "5/15/2008" "1/4/2008" "1/27/2008" ... $ Destination : Factor w/ 19 levels "AHC","AHG","AHH",..: 4 12 12 12 12 4 4 12 14 12 ... $ DischargeDay : Factor w/ 7 levels "FRI","MON","SAT",..: 6 5 1 4 2 1 6 6 7 7 ... $ los : int 1457 1329 1101 1048 280 249 158 218 43 54 ... $ NursingUnit : Factor w/ 34 levels "5MSG","5NFC",..: 5 4 3 3 3 5 4 4 3 5 ... $ Age : int 79 78 67 80 78 82 79 80 75 87 ... $ AdmitSource : Factor w/ 9 levels "EO","OS","OU",..: 6 6 6 6 6 6 6 6 6 6 ... $ DischargeDayNew: Factor w/ 7 levels "Monday","Tuesday",..: NA NA NA NA NA NA NA NA NA NA ...>table(data$DischargeDay)FRI MON SAT SUN THU TUE WED 1456 1249 769 473 1295 1368 1343>library(gdata) >data$DischargeDayNew <- reorder(data$DischargeDay,new.order=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"))>table(data$DischargeDayNew)Monday Tuesday Wednesday Thursday Friday Saturday Sunday 0 0 0 0 0 0 0 -- Christopher W. Ryan, MD SUNY Upstate Medical University Clinical Campus at Binghamton 40 Arch Street, Johnson City, NY 13790 cryanatbinghamtondotedu PGP public keys available at http://home.stny.rr.com/ryancw/ "If you want to build a ship, don't drum up the men to gather wood, divide the work and give orders. Instead, teach them to yearn for the vast and endless sea." [Antoine de St. Exupery]
Christopher W. Ryan
2008-Oct-30 21:38 UTC
[R] trying to figure out reorder.factor in gdata--solved--silly--
Original dataset used abbreviations for the day of week, while I was trying to use whole words :( Thanks anyway. Christopher W. Ryan, MD SUNY Upstate Medical University Clinical Campus at Binghamton 40 Arch Street, Johnson City, NY 13790 cryanatbinghamtondotedu PGP public keys available at http://home.stny.rr.com/ryancw/ "If you want to build a ship, don't drum up the men to gather wood, divide the work and give orders. Instead, teach them to yearn for the vast and endless sea." [Antoine de St. Exupery] Christopher W. Ryan wrote:> I'm trying to make a factor with levels for day of week, appear as > Monday-Sunday, rather than alphabetically. I'm using reorder.factor > from gdata package, but obviously not using it properly. What am I > doing wrong? Thanks. > > >> data <- read.table("DataEarly2008.txt", sep=" ", header=T, as.is=1:2) >> str(data) > 'data.frame': 7953 obs. of 9 variables: > $ AdmitDate : chr "6/28/2004" "9/24/2004" "12/29/2004" > "3/15/2005" ... > $ DischargeDate : chr "6/24/2008" "5/15/2008" "1/4/2008" "1/27/2008" ... > $ Destination : Factor w/ 19 levels "AHC","AHG","AHH",..: 4 12 12 12 > 12 4 4 12 14 12 ... > $ DischargeDay : Factor w/ 7 levels "FRI","MON","SAT",..: 6 5 1 4 2 1 > 6 6 7 7 ... > $ los : int 1457 1329 1101 1048 280 249 158 218 43 54 ... > $ NursingUnit : Factor w/ 34 levels "5MSG","5NFC",..: 5 4 3 3 3 5 4 > 4 3 5 ... > $ Age : int 79 78 67 80 78 82 79 80 75 87 ... > $ AdmitSource : Factor w/ 9 levels "EO","OS","OU",..: 6 6 6 6 6 6 6 > 6 6 6 ... > $ DischargeDayNew: Factor w/ 7 levels "Monday","Tuesday",..: NA NA NA > NA NA NA NA NA NA NA ... > > >> table(data$DischargeDay) > > FRI MON SAT SUN THU TUE WED > 1456 1249 769 473 1295 1368 1343 > >> library(gdata) >> data$DischargeDayNew <- reorder(data$DischargeDay, > new.order=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")) > >> table(data$DischargeDayNew) > Monday Tuesday Wednesday Thursday Friday Saturday Sunday > 0 0 0 0 0 0 0 > >