[R] urgent: question concerning data manipulation

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Mon Mar 4 17:28:55 CET 2013


Have a look at cast() from the reshape package.

library(reshape)
cast(personId ~ law, data = testdata, value = "article", fun = length)
cast(personId ~ law, data = testdata, value = "article", fun = function(x){1 * (length(x) > 0)})

________________________________________
Van: r-help-bounces op r-project.org [r-help-bounces op r-project.org] namens David Studer [studerov op gmail.com]
Verzonden: maandag 4 maart 2013 16:44
Aan: r-help op r-project.org
Onderwerp: [R] urgent: question concerning data manipulation

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 op 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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.



More information about the R-help mailing list