Elahe chalabi
2021-May-26 15:52 UTC
[R] Group by and add a constant value based on a condition dply
Hi everyone,
I have the following dataframe:?
? ? ? 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, 0L, 0L, 0L, 400L, 400L, 200L,?
? ? 800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020",
"3.01.2020",?
? ? "4.01.2020", "1.01.2020", "2.01.2020",
"3.01.2020", "4.01.2020",?
? ? "1.01.2020", "2.01.2020", "3.01.2020",
"4.01.2020", "1.01.2020",?
? ? "2.01.2020", "3.01.2020", "4.01.2020")), class
= "data.frame", row.names = c(NA,?
? ? ?-16L))
?using dplyr I need to group by "Depatment" and "Class" and
then for all the dates that are "4.01.2020"? and have the
"Value" greater than zero? add 5 to the "Value", meaning the
desired dataframe will be (NewValue column) :?
? ?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, 0L, 0L, 0L, 400L, 400L, 200L,?
?800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020",
"3.01.2020",?
?"4.01.2020", "1.01.2020", "2.01.2020",
"3.01.2020", "4.01.2020",?
?"1.01.2020", "2.01.2020", "3.01.2020",
"4.01.2020", "1.01.2020",?
?"2.01.2020", "3.01.2020", "4.01.2020"), NewValue
= c(0L, 100L,?
?800L, 805L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 405L, 200L, 800L,?
?1200L, 0L)), class = "data.frame", row.names = c(NA, -16L))
? ?
Thanks a lot for any help!
Elahe
Christopher W Ryan
2021-May-26 16:01 UTC
[R] [External Email] Group by and add a constant value based on a condition dply
Is the grouping beforehand necessary? Could you simply, "for all the dates that are "4.01.2020" and have the "Value" greater than zero add 5 to the "Value" "? I may be missing something. --Chris Ryan On Wed, May 26, 2021 at 11:53 AM Elahe chalabi via R-help < r-help at r-project.org> wrote:> Hi everyone, > > I have the following dataframe: > > > > 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, 0L, 0L, 0L, 400L, 400L, 200L, > 800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020", "3.01.2020", > "4.01.2020", "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", > "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", "1.01.2020", > "2.01.2020", "3.01.2020", "4.01.2020")), class = "data.frame", > row.names = c(NA, > -16L)) > > using dplyr I need to group by "Depatment" and "Class" and then for all > the dates that are "4.01.2020" and have the "Value" greater than zero add > 5 to the "Value", meaning the desired dataframe will be (NewValue column) : > > > > 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, 0L, 0L, 0L, 400L, 400L, 200L, > 800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020", "3.01.2020", > "4.01.2020", "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", > "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", "1.01.2020", > "2.01.2020", "3.01.2020", "4.01.2020"), NewValue = c(0L, 100L, > 800L, 805L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 405L, 200L, 800L, > 1200L, 0L)), class = "data.frame", row.names = c(NA, -16L)) > > > Thanks a lot for any help! > Elahe > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]