[R] Make 2nd col of 2-col df into header row of same df then adjust col1 data display
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Fri Dec 19 05:21:30 CET 2014
Please keep the list in the loop.
Take a look at my code again... the factor function can accept a vector of all levels you want it to include.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On December 18, 2014 7:35:28 PM PST, "Crombie, Burnette N" <bcrombie at utk.edu> wrote:
>Jeff, your code works fabulously on the dataset I submitted with my
>question, but I can't get it to retain all 14 of the PViol.Type's with
>my real dataset which is imported as a csv. Do you have any ideas how
>to fix this?
>
>##########
>MERGE_PViol.Detail.Per.Case <-
>read.csv("~/FOIA_FLSA/MERGE_PViol.Detail.Per.Case_for_rtf10.csv",
>stringsAsFactors=TRUE)
>
>###select only certain columns from dataset
>PViol.Type.Per.Case <- MERGE_PViol.Detail.Per.Case[,c("CaseID",
>"Primary.Viol.Type")]
>PViol.Type.Per.Case$CaseID <- factor(PViol.Type.Per.Case$CaseID)
>str(PViol.Type.Per.Case)
>### 'data.frame': 13 obs. of 2 variables:
>### $ CaseID : Factor w/ 8 levels "1005317","1007183",..: 5
>1 4 5 5 2 3 6 7 8 ...
>### $ Primary.Viol.Type: Factor w/ 5 levels "AS.Age","HS.Hours",..: 1 2
>2 2 4 3 3 3 3 3 ...
>
>
>PViol.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")
>
>###### Jeff Newmiller (RHelp)
>#PViol.Type.Per.Case <- data.frame( CaseID, Primary.Viol.Type=factor(
>Primary.Viol.Type, levels=PViol.Type ) )
>
>tmp <- table( PViol.Type.Per.Case )
>ans <- data.frame( CaseID=rownames( tmp ), as.data.frame( ifelse(
>0==tmp, NA, 1 ) ))
>##########
>
>
>
>-----Original Message-----
>From: Crombie, Burnette N
>Sent: Thursday, December 18, 2014 11:17 AM
>To: 'Jeff Newmiller'
>Subject: RE: [R] Make 2nd col of 2-col df into header row of same df
>then adjust col1 data display
>
>Thanks so much for your time. I will reply as soon as possible. I've
>been pulled away from my desk -- BNC
>
>-----Original Message-----
>From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.ca.us]
>Sent: Thursday, December 18, 2014 11:02 AM
>To: Crombie, Burnette N
>Cc: r-help at r-project.org
>Subject: Re: [R] Make 2nd col of 2-col df into header row of same df
>then adjust col1 data display
>
>No guarantees on "best"... but one way using base R could be:
>
># Note that "CaseID" is actually not a valid PViol.Type as you had it
>PViol.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" )
>
># explicitly specifying all levels to the factor insures a complete #
>set of column outputs regardless of what is in the input
>PViol.Type.Per.Case.Original <-
> data.frame( CaseID
> , Primary.Viol.Type=factor( Primary.Viol.Type
> , levels=PViol.Type ) )
>
>tmp <- table( PViol.Type.Per.Case.Original ) ans <- data.frame(
>CaseID=rownames( tmp )
> , as.data.frame( ifelse( 0==tmp, NA, 1 ) )
> )
>
>
>On Wed, 17 Dec 2014, bcrombie wrote:
>
>> # I have a dataframe that contains 2 columns:
>> CaseID <- c('1015285',
>> '1005317',
>> '1012281',
>> '1015285',
>> '1015285',
>> '1007183',
>> '1008833',
>> '1015315',
>> '1015322',
>> '1015285')
>>
>> Primary.Viol.Type <- c('AS.Age',
>> 'HS.Hours',
>> 'HS.Hours',
>> 'HS.Hours',
>> 'RK.Records_CL',
>> 'OT.Overtime',
>> 'OT.Overtime',
>> 'OT.Overtime',
>> 'V.Poster_Other',
>> 'V.Poster_Other')
>>
>> PViol.Type.Per.Case.Original <- data.frame(CaseID,Primary.Viol.Type)
>>
>> # CaseID?s can be repeated because there can be up to 14
>> Primary.Viol.Type?s per CaseID.
>>
>> # I want to transform this dataframe into one that has 15 columns,
>> where the first column is CaseID, and the rest are the 14 primary
>> viol. types. The CaseID column will contain a list of the unique
>> CaseID?s (no replicates) and for each of their rows, there will be a
>> ?1? under a column corresponding to a primary violation type
>recorded
>> for that CaseID. So, technically, there could be zero to 14 ?1?s? in
>a CaseID?s row.
>>
>> # For example, the row for CaseID '1015285' above would have a ?1?
>> under ?AS.Age?, ?HS.Hours?, ?RK.Records_CL?, and ?V.Poster_Other?,
>but have "NA"
>> under the rest of the columns.
>>
>> PViol.Type <- c("CaseID",
>> "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")
>>
>> PViol.Type.Columns <- t(data.frame(PViol.Type)
>>
>> # What is the best way to do this in R?
>>
>>
>>
>>
>> --
>> 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-tp4700878.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>---------------------------------------------------------------------------
>Jeff Newmiller The ..... ..... Go
>Live...
>DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
>Go...
> Live: OO#.. Dead: OO#.. Playing
>Research Engineer (Solar/Batteries O.O#. #.O#. with
>/Software/Embedded Controllers) .OO#. .OO#.
>rocks...1k
>---------------------------------------------------------------------------
More information about the R-help
mailing list