从R中的函数中提取结果/变量

时间:2017-05-19 07:47:24

标签: r function

我创建了一个函数来对我的数据集上的变量进行一些数据分析。该函数为每个变量计算diff。我想提取每个diff,然后创建一个包含所有vdiff

的向量diff
var = function(dt, title){
  m=count(dt)
  vlist=list()
  for (i in (1:dim(m)[1])){
    m$"%Renew"[i]=sum(data$Renew==1 & dt ==m$x[i])/m$freq[i] *100
  }

  diff=max(m[,3]) - min(m[,3])
  vlist=list(table=m, difference = diff)

  ggplot(m, aes(x,m[,3])) + geom_point()+
          theme(axis.text.x = element_text(angle = 60, hjust = 1), axis.title=element_text(size=14,face="bold")) +
          labs(x=title, y="% Renew") + ylim(0,100)
}


var(data$Product, "Product")
var(data$Purchase, "Purchase")
var(data$Type, "Type")
var(data$Freq, "Freq")
var(data$Gender, "Gender")
var(data$State, "State")
var(data$Income, "Income")
var(data$Marital1, "Marital1")
var(data$Children, "Children")

var(data$Children, "Children")$diff在这里不起作用。我还看一下str(var(data$Children, "Children")),仍然无法弄清楚如何从函数中确定diff。理想情况下,我想编写代码,为每个变量赋予diff的值。

2 个答案:

答案 0 :(得分:1)

你需要在最后返回差异

var = function(dt, title){
  m=count(dt)
  vlist=list()
  for (i in (1:dim(m)[1])){
    m$"%Renew"[i]=sum(data$Renew==1 & dt ==m$x[i])/m$freq[i] *100
  }

  diff=max(m[,3]) - min(m[,3])
  vlist=list(table=m, difference = diff)

  print(ggplot(m, aes(x,m[,3])) + geom_point()+
          theme(axis.text.x = element_text(angle = 60, hjust = 1), axis.title=element_text(size=14,face="bold")) +
          labs(x=title, y="% Renew") + ylim(0,100))
  return(diff)
}

然后得到你的向量,假设dt是data.frame:

cols <- c("Product", "Purchase","Type","Freq","Gender","State","Income","Marital1","Children")

vdiff <- sapply(cols,function(x){var(dt[,x],x)})

另外,var和diff是基本R函数,选择其他名称可能是明智的。

答案 1 :(得分:0)

您的函数返回ggplot2个对象。考虑返回其他一些对象,例如return(vlist)。您可以从difference变量中部分匹配值tablevlist

如果这是您函数的最后一行,则无需明确命名return()