[R] Group by and duplicate a value/dplyr

Gerrit Eichner gerr|t@e|chner @end|ng |rom m@th@un|-g|e@@en@de
Tue May 11 15:10:45 CEST 2021


Hello, Elahe,

you were, of course, supposed to insert my suggested
code-snippet into you code and test it therein ...

  Regards  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
gerrit.eichner using math.uni-giessen.de   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
http://www.uni-giessen.de/eichner
---------------------------------------------------------------------

Am 11.05.2021 um 13:40 schrieb Elahe chalabi:
> Hello Gerit
> 
> mutate(MinValue = min(Value[Value != 0]) )  or  mutate(MinValue = sort(unique(Value))[2]) only mutates one value which is 100, it doesnt mutate minimum Value != 0 per group by element
> 
> 
> 
> 
> 
> 
> 
> 
> On Tuesday, May 11, 2021, 01:26:49 PM GMT+2, Gerrit Eichner <gerrit.eichner using math.uni-giessen.de> wrote:
> 
> 
> 
> 
> 
> Homework?
> 
> Try maybe
> 
> mutate(MinValue = min(Value[Value != 0]) )
> 
> or
> 
> mutate(MinValue = sort(unique(Value))[2])
> 
>    Hth  --  Gerrit
> 
> ---------------------------------------------------------------------
> Dr. Gerrit Eichner                  Mathematical Institute, Room 212
> gerrit.eichner using math.uni-giessen.de  Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> http://www.uni-giessen.de/eichner
> ---------------------------------------------------------------------
> 
> Am 11.05.2021 um 13:11 schrieb Elahe chalabi via R-help:
>> 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
> 
>>
>> ______________________________________________
>> R-help using r-project.org 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.
>>
> 
> ______________________________________________
> R-help using r-project.org 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.
>



More information about the R-help mailing list