Group by and duplicate a value/dplyr

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Group by and duplicate a value/dplyr

R help mailing list-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Group by and duplicate a value/dplyr

Gerrit Eichner
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

FW: Group by and duplicate a value/dplyr

PIKAL Petr
In reply to this post by R help mailing list-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Group by and duplicate a value/dplyr

Rui Barradas
In reply to this post by R help mailing list-2
Hello,

This can be done by getting the min of Value[Value != 0].
In the code that follows I have named the expected output df2 and
assigned the result to df3 and df4.


library(dplyr)

df3 <- df %>%
   group_by(Department,Class) %>%
   mutate(flag = Value != 0,
     MinValue = min(Value[flag]) ) %>%
   select(-flag)

identical(df2$MinValue, df3$MinValue)
#[1] TRUE


Or, simpler:


df4 <- df %>%
   group_by(Department,Class) %>%
   mutate(MinValue = min(Value[Value != 0]) )

identical(df2$MinValue, df4$MinValue)
#[1] TRUE


Hope this helps,

Rui Barradas



Às 12:11 de 11/05/21, Elahe chalabi via R-help escreveu:

> Hi all,
>
> I have the following data frame
>
>
> dput(df)
>      structure(list(Department = c("A", "A", "A", "A", "A", "A", "A",
> "A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L,
> 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L,
> 100L, 800L, 800L, 0L, 300L, 1200L, 1200L, 0L, 0L, 400L, 400L,
> 200L, 800L, 1200L, 1200L)), class = "data.frame", row.names = c(NA,
> -16L))
>
>
> I would like to group by "Department" and "Class" and repeat the minimum value of "Valule" excluding zeros or get the second minimum value. The desired output is:
>
>
>      dput(df)
>      structure(list(Department = c("A", "A", "A", "A", "A", "A", "A",
> "A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L,
> 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L,
> 100L, 800L, 800L, 0L, 300L, 1200L, 1200L, 0L, 0L, 400L, 400L,
> 200L, 800L, 1200L, 1200L), MinValue = c(100L, 100L, 100L, 100L,
> 300L, 300L, 300L, 300L, 400L, 400L, 400L, 400L, 200L, 200L, 200L,
> 200L)), class = "data.frame", row.names = c(NA, -16L))
>
>    
> how should I change the following dplyr to give me the desired output?
>
>
>     df <-
>    df %>%
>    group_by(Department,Class) %>%
>    mutate(MinValue=min(Value) )
>
>
> Thanks for any help.
> Elahe
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Group by and duplicate a value/dplyr

R help mailing list-2
In reply to this post by Gerrit Eichner
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: FW: Group by and duplicate a value/dplyr

R help mailing list-2
In reply to this post by PIKAL Petr
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Group by and duplicate a value/dplyr

Gerrit Eichner
In reply to this post by R help mailing list-2
CONTENTS DELETED
The author has deleted this message.