简体   繁体   中英

R mutate ifelse update conditional row with calculated function value

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

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))

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM