Skip to content
Prev 318686 / 398503 Next

urgent: question concerning data manipulation

Here is one way. There will be many ways to do it; I offer this one
because it is very general.

-Don


tmp <- split(testdata, testdata$personId)

myfun <- function(df) {
  dfo <- df
  if (any(df$law=='SVG')) dfo$svg <- 1 else dfo$svg <- 0
  dfo
}

tmpo <- lapply(tmp,myfun)

testout <- do.call('rbind', tmpo)
personId  law article svg
1.1         1  SVG      10   1
1.2         1  SVG      10   1
2.3         2 StGB     123   1
2.4         2 StGB     122   1
2.5         2  SVG      10   1
2.6         2  AuG      40   1
2.7         2 StGB     126   1
3           3  SVG      10   1
4.9         4 StGB     111   0
4.10        4  AuG      40   0