[R] to modify a dataframe

2014-01-01 Thread Arnaud Michel

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,
0, 0, 0, 1, 0, 1), Pays5 = c(1, 1, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Nom",
"Pays1", "Pays2", "Pays3", "Pays4", "Pays5"), row.names = c(1L,
3L, 4L, 2L, 5L, 6L, 7L, 8L, 9L), class = "data.frame")


I look for a way to build the new dataframe df2
 
df2 <-

structure(list(Nom = structure(1:9, .Label = c("A1", "A2", "A3",
"B1", "B2", "C1", "C2", "C3", "C4"), class = "factor"), Pays1 = c(1,
1, 1, 1, 1, 0, 0, 0, 0), Pays2 = c(0, 0, 0, 1, 1, 1, 1, 1, 1),
Pays3 = c(0, 0, 0, 1, 1, 0, 0, 0, 0), Pays4 = c(1, 1, 1,
0, 0, 1, 1, 1, 1), Pays5 = c(1, 1, 1, 0, 0, 0, 0, 0, 0)), .Names = c("Nom",
"Pays1", "Pays2", "Pays3", "Pays4", "Pays5"), row.names = c(NA,
-9L), class = "data.frame")

The purpose is to transform df1 it df2 by giving for every group of lines A, B 
and C the value 1 if there is at least a value equal to 1 or a value 0 if there 
is no value equal to 1

Thanks for your helps

--
Michel ARNAUD
Chargé de mission auprès du DRH
DGDRD-Drh - TA 174/04
Av Agropolis 34398 Montpellier cedex 5
tel : 04.67.61.75.38
fax : 04.67.61.57.87
port: 06.47.43.55.31

__
R-help@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.


Re: [R] to modify a dataframe

2014-01-01 Thread Bert Gunter
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 groups you have specified:

grp <- factor(substring(df1$Nom,1,1))

Note that you may need to use regular expressions or some other method
to do this if your naming system is more complex than you have shown.

Step 2: Create your new structure:

df2 <- df1
df2[,-1]<-lapply(df1[-1],function(x)ave(x,grp,FUN=max))

HTH.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch




On Wed, Jan 1, 2014 at 8:55 AM, Arnaud Michel  wrote:
> 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,
> 0, 0, 0, 1, 0, 1), Pays5 = c(1, 1, 0, 0, 0, 0, 0, 0, 0)), .Names =
> c("Nom",
> "Pays1", "Pays2", "Pays3", "Pays4", "Pays5"), row.names = c(1L,
> 3L, 4L, 2L, 5L, 6L, 7L, 8L, 9L), class = "data.frame")
>
>
> I look for a way to build the new dataframe df2
>  df2 <-
> structure(list(Nom = structure(1:9, .Label = c("A1", "A2", "A3",
> "B1", "B2", "C1", "C2", "C3", "C4"), class = "factor"), Pays1 = c(1,
> 1, 1, 1, 1, 0, 0, 0, 0), Pays2 = c(0, 0, 0, 1, 1, 1, 1, 1, 1),
> Pays3 = c(0, 0, 0, 1, 1, 0, 0, 0, 0), Pays4 = c(1, 1, 1,
> 0, 0, 1, 1, 1, 1), Pays5 = c(1, 1, 1, 0, 0, 0, 0, 0, 0)), .Names =
> c("Nom",
> "Pays1", "Pays2", "Pays3", "Pays4", "Pays5"), row.names = c(NA,
> -9L), class = "data.frame")
>
> The purpose is to transform df1 it df2 by giving for every group of lines A,
> B and C the value 1 if there is at least a value equal to 1 or a value 0 if
> there is no value equal to 1
>
> Thanks for your helps
>
> --
> Michel ARNAUD
> Chargé de mission auprès du DRH
> DGDRD-Drh - TA 174/04
> Av Agropolis 34398 Montpellier cedex 5
> tel : 04.67.61.75.38
> fax : 04.67.61.57.87
> port: 06.47.43.55.31
>
> __
> R-help@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.

__
R-help@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.


Re: [R] to modify a dataframe

2014-01-01 Thread Rui Barradas

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 <- do.call(rbind, lst1)
rownames(df3) <- NULL

identical(df2, df3)  # TRUE


Hope this helps,

Rui Barradas

Em 01-01-2014 16:55, Arnaud Michel escreveu:

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,
 0, 0, 0, 1, 0, 1), Pays5 = c(1, 1, 0, 0, 0, 0, 0, 0, 0)), .Names =
c("Nom",
"Pays1", "Pays2", "Pays3", "Pays4", "Pays5"), row.names = c(1L,
3L, 4L, 2L, 5L, 6L, 7L, 8L, 9L), class = "data.frame")


I look for a way to build the new dataframe df2

df2 <-
structure(list(Nom = structure(1:9, .Label = c("A1", "A2", "A3",
"B1", "B2", "C1", "C2", "C3", "C4"), class = "factor"), Pays1 = c(1,
1, 1, 1, 1, 0, 0, 0, 0), Pays2 = c(0, 0, 0, 1, 1, 1, 1, 1, 1),
 Pays3 = c(0, 0, 0, 1, 1, 0, 0, 0, 0), Pays4 = c(1, 1, 1,
 0, 0, 1, 1, 1, 1), Pays5 = c(1, 1, 1, 0, 0, 0, 0, 0, 0)), .Names =
c("Nom",
"Pays1", "Pays2", "Pays3", "Pays4", "Pays5"), row.names = c(NA,
-9L), class = "data.frame")

The purpose is to transform df1 it df2 by giving for every group of
lines A, B and C the value 1 if there is at least a value equal to 1 or
a value 0 if there is no value equal to 1

Thanks for your helps



__
R-help@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.


Re: [R] to modify a dataframe

2014-01-01 Thread arun
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:
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,
     0, 0, 0, 1, 0, 1), Pays5 = c(1, 1, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Nom",
"Pays1", "Pays2", "Pays3", "Pays4", "Pays5"), row.names = c(1L,
3L, 4L, 2L, 5L, 6L, 7L, 8L, 9L), class = "data.frame")


I look for a way to build the new dataframe df2
  
df2 <-
structure(list(Nom = structure(1:9, .Label = c("A1", "A2", "A3",
"B1", "B2", "C1", "C2", "C3", "C4"), class = "factor"), Pays1 = c(1,
1, 1, 1, 1, 0, 0, 0, 0), Pays2 = c(0, 0, 0, 1, 1, 1, 1, 1, 1),
     Pays3 = c(0, 0, 0, 1, 1, 0, 0, 0, 0), Pays4 = c(1, 1, 1,
     0, 0, 1, 1, 1, 1), Pays5 = c(1, 1, 1, 0, 0, 0, 0, 0, 0)), .Names = c("Nom",
"Pays1", "Pays2", "Pays3", "Pays4", "Pays5"), row.names = c(NA,
-9L), class = "data.frame")

The purpose is to transform df1 it df2 by giving for every group of lines A, B 
and C the value 1 if there is at least a value equal to 1 or a value 0 if there 
is no value equal to 1

Thanks for your helps

-- 
Michel ARNAUD
Chargé de mission auprès du DRH
DGDRD-Drh - TA 174/04
Av Agropolis 34398 Montpellier cedex 5
tel : 04.67.61.75.38
fax : 04.67.61.57.87
port: 06.47.43.55.31


__
R-help@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.

__
R-help@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.