Re: [R] Group by and duplicate a value/dplyr

2021-05-11 Thread Gerrit Eichner
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.eich...@m

Re: [R] Group by and duplicate a value/dplyr

2021-05-11 Thread Elahe chalabi via R-help
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 wrote: Homework? Try ma

Re: [R] Group by and duplicate a value/dplyr

2021-05-11 Thread Rui Barradas
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]) ) %

Re: [R] Group by and duplicate a value/dplyr

2021-05-11 Thread Gerrit Eichner
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.eich...@math.uni-gie

[R] Group by and duplicate a value/dplyr

2021-05-11 Thread 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, 1200