在R中创建函数时出错

时间:2018-05-08 09:28:33

标签: r function aggregate

我正在尝试创建一个函数,因为我需要多次将相同的代码应用到数据中的不同列。

我的数据(df)如下所示:

  WEEK1.x WEEK1.y WEEK2.x WEEK2.y WEEK3.x WEEK3.y
1  660.14       1  690.74       2  821.34       1
2 -482.89      99 -368.12      99 -368.12      99
3  284.48       3  399.90       1  375.32       1
4 -554.18      99 -300.28      99 -300.28      99

然后我的功能看起来像:

  extra<-function(first_var, second_var){
  df$first_var=ifelse((df$first_var == 99),"99",
                      ifelse((df$first_var %in% c(1,2,3,4,5)),"1-5",NA))
  output=as.data.frame(aggregate(second_var~first_var, data = df, mean))
  return(output)
 }
  WEEK1<-extra("WEEK1.y", "WEEK1.x")
  WEEK2<-extra("WEEK2.y", "WEEK2.y")

这给了我错误:

  

$<-.data.frame*tmp*,first_var,value = logical(0))出错:
  替换有0行,数据有1416

当我按下查看回溯时,这就是它所说的:

  
      
  1. 停止(sprintf(ngettext(N,&#34;替换有%d行,数据有%d&#34;,   &#34;替换有%d行,数据有%d&#34;),N,nrows),域= NA)
  2.   
  3. $<-.data.frame*tmp*,first_var,value = logical(0))
  4.   
  5. $<-*tmp*,first_var,value = logical(0))
  6.   
  7. extra(&#34; WEEK1.y&#34;,&#34; WEEK1.x&#34;)
  8.   

我不确定问题是什么?

1 个答案:

答案 0 :(得分:2)

这是您的功能的工作版本 我使用了@ A.Suliman提出的建议的变体,但使用了[[

extra <- function(first_var, second_var){
  df[[first_var]] <- ifelse((df[[first_var]] == 99), "99",
                      ifelse((df[[first_var]] %in% c(1,2,3,4,5)), "1-5", NA))
  fmla <- as.formula(paste(second_var, first_var, sep = "~"))
  aggregate(fmla, data = df, mean, na.rm = TRUE)
}

WEEK1 <- extra("WEEK1.y", "WEEK1.x")
WEEK1
#  WEEK1.y  WEEK1.x
#1     1-5  472.310
#2      99 -518.535


WEEK2 <- extra("WEEK2.y", "WEEK2.x")
WEEK2
#  WEEK2.y WEEK2.x
#1     1-5  545.32
#2      99 -334.20

请注意,我还建议您将df作为参数传递给函数。依赖于在函数环境中存在的其他对象通常被认为是不好的做法。在这种情况下,df中存在.GlobalEnv,您迫使R离开需要找到它的环境。

数据。

df <- read.table(text = "
  WEEK1.x WEEK1.y WEEK2.x WEEK2.y WEEK3.x WEEK3.y
1  660.14       1  690.74       2  821.34       1
2 -482.89      99 -368.12      99 -368.12      99
3  284.48       3  399.90       1  375.32       1
4 -554.18      99 -300.28      99 -300.28      99
", header = TRUE)