0

Let's say we have the following data frame:

# Data
Id <- c(1,2,3,4,5,6,7,8,9,10)
Type <- c("Beginner", "Expert", "Intermediate", "Beginner", 
  "Professional", "Expert", "Intermediate", "Professional", 
  "Professional", "Expert")
Response<- c(1,1,2,2,1,2,1,2,1,1)
Successful <- data.frame(Id, Type, Response)
Successful

# Dataframe
#   Successful
Id  Type             Response    
1   Beginner         1
2   Expert           1
3   Intermediate     2
4   Beginner         2
5   Professional     1
6   Expert           2
7   Intermediate     1
8   Professional     2
9   Professional     1
10  Expert           1

I know that I could store it as an object (DFRespType) in the global environment by doing the following:

 DFRespType <- 
  as.data.frame(round(100*prop.table(table(Successful$Response, 
                                   Successful$Type),2), 1))

But instead, I would like to create a function for storing the object to make doing this a lot more efficient. Below I tried to make the StoreDF function:

StoreDF <- function(DFName, dataset, variable1, variable2){
  DFName <- as.data.frame(round(100*prop.table(table(dataset$variable1, 
                                              dataset$variable2),2), 1))
}

But when I try and use it in the following way, nothing is stored:

StoreDF(DFRespType, Successful, Response, Type)

Any help with this would be gratefully appreciated.

  • Try the `< – Argon Jun 27 '19 at 00:19
  • 1
    This is a misuse of functions. You are trying to produce an object through side effect. Don't do it. You might want to look into storing multiple data.frames in a list. See this post: [make list of data.frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames). – lmo Jun 27 '19 at 00:30

1 Answers1

2

Don't store objects in global environment from inside the function. Instead return the dataframe back from the function. Also using quoted variables to subset the dataframe.

StoreDF <- function(dataset, variable1, variable2){
    as.data.frame(round(100* prop.table(table(dataset[[variable1]], 
                        dataset[[variable2]]),2), 1))
}

DFRespType <- StoreDF(Successful, "Response", "Type")
DFRespType

#  Var1         Var2 Freq
#1    1     Beginner 50.0
#2    2     Beginner 50.0
#3    1       Expert 66.7
#4    2       Expert 33.3
#5    1 Intermediate 50.0
#6    2 Intermediate 50.0
#7    1 Professional 66.7
#8    2 Professional 33.3
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
  • Thanks Ronak, this works well. Could you explain why objects shouldn't be stored in the global environment from inside functions? And why have you used double instead of single square brackets? – interestedindata Jun 27 '19 at 18:53
  • @interestedindata If you want to store objects from inside the function in global environment you have to use `assign` or `< – Ronak Shah Jun 28 '19 at 01:35