0
votes

I am use R mutate to update a specific (conditional) row with a calculated function, namely, nrow(), to update with an add (+) value. I cannot use apply() as I need to update only one (1) row for a specific value.

For example, when find row Year==2007 and Month==06, add Incoming.Exam + nrow(df3), so that row will be 698+nrow value.

I get the following error from mutate impl:

Error in mutate_impl(.data, dots) : Column abberville_LA must be length 96 (the number of rows) or one, not 4

    abberville_LA %>% 
  mutate(abberville_LA, Incoming.Exam = ifelse(abberville_LA$Year == 2007 & abberville_LA$Month == 06, abberville_LA, Incoming.Exam + nrow(abberville_df3), abberville_LA$Incoming.Exam))

    head(abberville_LA, 3)
  Incoming.Exam Year Month    ts_date
1           698 2007     6    2007-06-01
2           NaN 2010     6    2010-06-01
2
Do not use $ in mutate. - NelsonGon
in other words, just use the field name, eg, Year, Month - user1857373
I still get: Error in mutate_impl(.data, dots) : Column abberville_LA must be length 96 (the number of rows) or one, not 4 - user1857373
abberville_LA %>% mutate(abberville_LA, Incoming.Exam = ifelse(Year == 2007 & Month == 05, Incoming.Exam + nrow(abberville_df3), Incoming.Exam)) - user1857373
We do not have your data to exactly say what is going on but if you are chaining abberville_LA %>% ... you don't need to add abberville_LA again in mutate as first argument. - Ronak Shah

2 Answers

1
votes

1 .Your question is not clear , So I am trying to apprehend what you want and answering the question

2 .You are using $ in mutate which is not required . Running the below code should solve the issue .

abberville_LA %>% 
  mutate(Incoming.Exam = ifelse(Year == '2007' & Month == '06', Incoming.Exam + nrow(abberville_df3),Incoming.Exam))
0
votes

the issue was the library dplyr. I discovered that I had an slightly older version and needed to update to resolve the "Error in mutate_impl(.data, dots) : Evaluation error: as_dictionary() is defunct as of rlang 0.3.0. Please use as_data_pronoun() instead" error message, which was pointing out that another version of dplyr should be utilized. This fixed the code that was provided as answers on this forum.