user129343
user129343

Reputation: 47

extract a result/variable from a function in R

I create a function to do some data analysis on variables on my dataset. The function calculates diff for each variable. I want to extract eachdiff, then created a vector vdiff consists of all 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 does not work here. I also takes a look at str(var(data$Children, "Children")), still cannot figure out how to exact diff from the function. Ideally, I want to write code to give me the value of diff for each variable.

Upvotes: 0

Views: 3217

Answers (2)

moodymudskipper
moodymudskipper

Reputation: 47320

You need to return diff in the end

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

then to get your vector, assuming dt is a data.frame:

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

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

Also, var and diff are base R functions, it may be wise to choose other names.

Upvotes: 2

Roman Luštrik
Roman Luštrik

Reputation: 70643

Your function returns ggplot2 object. Consider returning some other object, like return(vlist). You will be able to partially match values difference and table from your vlist variable.

Explicitly naming return() is not necessary if this is the last line of your function.

Upvotes: 0

Related Questions