R Have a function create a dataframe with part of the data passed in as part of the name

29 views Asked by At

I know the title isn't great, but it's hard to put into words what I am trying to do. What I'd like it s function that does something like this:

stats = function(twtdata) {
react = twtdata %>% select(favoriteCount, retweetCount)
X1 = mean(react$favoriteCount)
X2 = mean(react$retweetCount)
X3 = nrow(react)
}

twtdata is a string. I want X1 to be twtdataFavoriteCount, X2 to be twtdataRetweetCount and X3 to be twtdataTweets, where twtdata is the string being passed into the function, kind of like INNERJOIN in excel, or string concatenation.

1

There are 1 answers

0
Ronak Shah On

If you are passing dataframe name as string try using get :

stats = function(twtdata) {
  twtdata <- get(twtdata, envir = parent.frame())
  react = twtdata %>% select(favoriteCount, retweetCount)
  X1 = mean(react$favoriteCount)
  X2 = mean(react$retweetCount)
  X3 = nrow(react)
}