[R] Make 2nd col of 2-col df into header row of same df then adjust col1 data display

bcrombie bcrombie at utk.edu
Mon Dec 22 05:32:12 CET 2014

Here's the answer to my question that I came up with after working with all
of your suggestions.  It's not elegant by any means but gets the job done:

MERGE_PViol.Detail.Per.Case <-
# select only certain columns from original dataset
PViol.Type.Per.Case <- MERGE_PViol.Detail.Per.Case[,c("CaseID",

# create a dataframe with all the primary violation types possible
Primary.Viol.Type <- c("BW.BackWages",

CaseID  <- c('1','2','3','4','5','6','7','8','9','10','11','12','13','14')
PViol.Type.df <- data.frame(CaseID, Primary.Viol.Type)

# merge original data with primary viol type template dataframe
df<- merge(PViol.Type.Per.Case, PViol.Type.df, all=TRUE)
df1<-dcast(df, df[,1] ~ df[,2])

# delete rows with template CaseID's;
# replace strings with "1"
colnames(df1)[1] <- "CaseID"
remove <- c('1','2','3','4','5','6','7','8','9','10','11','12','13','14')
rownames(df1) <- df1$CaseID
df2 <- df1[!df1$CaseID %in% remove, ]
df2[!is.na(df2)] <- 1
df2$CaseID <- rownames(df2)

View this message in context: http://r.789695.n4.nabble.com/Make-2nd-col-of-2-col-df-into-header-row-of-same-df-then-adjust-col1-data-display-tp4700878p4701020.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list