[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 <-
read.csv("~/FOIA_FLSA/MERGE_PViol.Detail.Per.Case_for_rtf10.csv",
stringsAsFactors=TRUE)
# select only certain columns from original dataset
PViol.Type.Per.Case <- MERGE_PViol.Detail.Per.Case[,c("CaseID",
"Primary.Viol.Type")]

# create a dataframe with all the primary violation types possible
Primary.Viol.Type <- c("BW.BackWages",
                "LD.Liquid_Damages",
                "MW.Minimum_Wage",
                "OT.Overtime",
                "RK.Records_FLSA",
                "V.Poster_Other",
                "AS.Age",
                "BW.WHMIS_BackWages",
                "HS.Hours",
                "OA.HazOccupationAg",
                "ON.HazOccupationNonAg",
                "R3.Reg3AgeOccupation",
                "RK.Records_CL",
                "V.Other")

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)
library(reshape2)
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