Hello,

I too don't see why grouping is needed but here it goes.


df1 %>%
  group_by(Department, Class) %>%
  mutate(Value = Value + 5*(Date == "4.01.2020"))


Hope this helps,

Rui Barradas

Às 17:01 de 26/05/21, Christopher W Ryan via R-help escreveu:
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@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@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]]

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

Reply via email to