用计算的函数值对ifelse更新条件行进行变异 [英] R mutate ifelse update conditional row with calculated function value

查看:47
本文介绍了用计算的函数值对ifelse更新条件行进行变异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R mutate通过计算函数 nrow()更新特定(条件)行,以添加(+)值进行更新.我不能使用 apply(),因为我只需要为特定值更新一(1)行即可.

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.

例如,当查找Year == 2007和Month == 06行时,添加Incoming.Exam + nrow(df3),这样该行将为698 + nrow值.

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

我从mutate impl中收到以下错误:

I get the following error from mutate impl:

mutate_impl(.data,点)中的错误:列 abberville_LA 的长度必须为96(行数)或1,而不是4

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.您的问题不清楚,所以我试图理解您想要的内容并回答问题

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

2.您正在mutate中使用$,这不是必需的.运行以下代码应该可以解决问题.

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

这篇关于用计算的函数值对ifelse更新条件行进行变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆