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

John Kane jrkrideau at inbox.com
Thu Dec 18 16:05:20 CET 2014


Of course, but why? As Brian S says you have not given us enough information to know exactly what you are after. 

Have a look at https://github.com/hadley/devtools/wiki/Reproducibility or http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for some information on how to form a question for the list.

It is good that you provided some data but it is better to use dput() (see links above or ?dput) to supply the data as different R users have different settings on their systems and may not read that data in the same way. 

Note that I have simplified your incredibly verbose names and put everything into lower case (see ?tolower) just to make life easier. Because R is case-sensitive, it is usually easier to keep to lower case as much as possible particularly when posting to the list and to use simple variable names where the actual variables are likely to meaningless to the reader and long upper case names just makes for more typing.

In any case here is a quick and dirty semi-solution using the reshape2 package which I imagine you will have to install using “install.packages("reshape2"). 

Depending on exactly what you need to know there may be, as Brian S says many different and better approaches. While we really don't need the actual variable names we need an overall idea of what you are going in substantive terms and what the final results are. 

Anyway welcome to the R-help list

#========start code=====
library(reshape2)
dat1  <-  structure(list(id = structure(c(5L, 1L, 4L, 5L, 5L, 2L, 3L, 6L, 
7L, 5L), .Label = c("1005317", "1007183", "1008833", "1012281", 
"1015285", "1015315", "1015322"), class = "factor"), type = structure(c(1L, 
2L, 2L, 2L, 4L, 3L, 3L, 3L, 5L, 5L), .Label = c("as.age", "hs.hours", 
"ot.overtime", "rk.records_cl", "v.poster_other"), class = "factor")), .Names = c("id", 
"type"), row.names = c(NA, -10L), class = "data.frame")
  

dcast(dat1, id ~ type)

#=======end code =======

John Kane
Kingston ON Canada


> -----Original Message-----
> From: bcrombie at utk.edu
> Sent: Wed, 17 Dec 2014 19:15:14 -0800 (PST)
> To: r-help at r-project.org
> Subject: [R] Make 2nd col of 2-col df into header row of same df then
> adjust col1 data display
> 
> # 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.

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!



More information about the R-help mailing list