Hi,
You could try:
df3 <- df1
library(plyr)
df3[,-1] <- ddply(df1,.(Nom1=gsub("\\d+","",Nom)),colwise(function(x)
rep(max(x),length(x[,-1]
attr(df3,"row.names") <- attr(df2,"row.names")
identical(df2,df3)
#[1] TRUE
A.K.
On Wednesday, January 1, 2014 11:56 AM, Arnaud Michel
wrote:
Dea
Hello,
Here's one way.
lst1 <- lapply(split(df1, gsub("[0-9]", "", df1$Nom)), function(x){
x[, -1] <- lapply(x[, -1], function(y){
z <- if(any(y == 1)) 1 else 0
rep(z, length(y))
})
x
})
df3
1. Thank you for the clear reproducible example. This made it easy to
see what you wanted and provide an answer. Hopefully a correct one!
2. Many ways to do this. Here's one, but others may be better.
Step1: First greate a grouping factor for Nom to group the separate
row labels into the logical
Dear All,
From the dataframe df1
df1 <-
structure(list(Nom = structure(1:9, .Label = c("A1", "A2", "A3",
"B1", "B2", "C1", "C2", "C3", "C4"), class = "factor"), Pays1 = c(1,
1, 0, 0, 1, 0, 0, 0, 0), Pays2 = c(0, 0, 0, 1, 1, 0, 1, 0, 1),
Pays3 = c(0, 0, 0, 0, 1, 0, 0, 0, 0), Pays4 = c(1, 0, 0
4 matches
Mail list logo