[R] urgent: question concerning data manipulation

Eik Vettorazzi E.Vettorazzi at uke.de
Mon Mar 4 17:42:20 CET 2013


There’s more than one way to skin a cat, here is another

mm<-model.matrix(~personId+law+0,testdata)
merge(testdata,aggregate(mm[,-1],list(personId=mm[,"personId"]),max))

cheers

Am 04.03.2013 16:44, schrieb David Studer:
> Hello everyone!
> 
> Does anyone of you know how I could solve the following problem.
> I guess, it is not a very difficult question, but I simply lack of the
> right idea:
> 
> I have a dataset containing data of convictions. This dataset contains 4
> columns:
> - personId: individual number that identifies the offender
> - law: law which has been violated
> - article: article which has been violated
> 
> # Testdata:
> personId<-c(1,1,2,2,2,2,2,3,4,4)
> law<-c("SVG", "SVG", "StGB", "StGB", "SVG", "AuG", "StGB", "SVG", "StGB",
> "AuG")
> article<-c(10, 10, 123, 122, 10, 40, 126, 10, 111, 40)
> testdata<-data.frame(personId, law, article)
> 
> Now I'd like to create three additional dummy-coded columns for each law
> (SVG, StGB, AuG).
> For each offender (all offenders have the same personId) it should be
> checked, whether there are
> any violations against the three laws. If there are any violations against
> SVG (for example), then
> in all rows of this offender the column SVG should have the value 1
> (otherwise 0).
> 
> For example offender 2 has once violated against law "SVG" therefore his
> four entries should have
> the value 1 at the column "SVG".
> 
> I hope you can understand my problem. I'd really appreciate any hints and
> solutions!
> 
> Thank you!
> David
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
> 


-- 
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790
--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus



More information about the R-help mailing list