简体   繁体   English

用计算的函数值对ifelse更新条件行进行变异

[英]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. 我使用R mutate通过计算函数nrow()更新特定(条件)行,以添加(+)值更新。 I cannot use apply() as I need to update only one (1) row for a specific value. 我无法使用apply()因为我只需要为特定值更新一(1)行。

For example, when find row Year==2007 and Month==06, add Incoming.Exam + nrow(df3), so that row will be 698+nrow value. 例如,当查找行Year == 2007和Month == 06时,添加Incoming.Exam + nrow(df3),这样该行将为698 + nrow值。

I get the following error from mutate impl: 我从mutate impl中收到以下错误:

Error in mutate_impl(.data, dots) : Column abberville_LA must be length 96 (the number of rows) or one, not 4 mutate_impl(.data,点)中的错误: abberville_LA列的abberville_LA必须为96(行数)或一个,而不是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 1。您的问题不清楚,所以我试图理解您想要什么并回答问题

2 .You are using $ in mutate which is not required . 2。您在不需要的mutate中使用$。 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. 问题是图书馆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. 我发现我有一个稍旧的版本,需要更新以解决“ mutate_impl(.data,点)中的错误:评估错误:从rlang 0.3.0起,as_dictionary()已失效。请改用as_data_pronoun()”错误消息,指出应该使用dplyr的另一个版本。 This fixed the code that was provided as answers on this forum. 这修复了该论坛上作为答案提供的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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