Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204922

Is there a way to give each subset from a dataframe a different object name within an R function?

$
0
0

I need to make different subsets from one data.frame, and then use these different subsets for subsequent actions. Therefore, I need to give each subset a unique name. I tried writing different functions but could not figure out how.
Here I show the function for one subset, i.e. datsub:

run_subsets <- function(data, t) {
  datsub <- data[eval(substitute(t),data), ]
}
run_subsets(data=dat, t=type=="AM", x=datsub1)

But I need to have different names for each different subset, so next to *datsub", e.g.,
datsub1datsub2, etc What I tried already:

run_subsets <- function(data, t, x) {
  datsub <- data[eval(substitute(t),data), ]
  x <- datasub
}
run_subsets(data=dat, t=type=="AM", x=datsub1)
run_subsets <- function(data, t, x) {
  datsub <- data[eval(substitute(t),data), ]
  gsub("b", "x", "datsub")
run_subsets(data=dat, t=type=="AM", x=b1)
run_subsets <- function(data, t, x) {
  datsub <- data[eval(substitute(t),data), ]
  list(substitute(x)) <- datsub
run_subsets(data=dat, t=type=="AM", x=datsub1)
run_subsets <- function(data, t, x) {
  datsub <<- data[eval(substitute(t),data), ]
  data_name <- paste("datsub", x, sep=".")
  assign(data_name, datsub)
  save(list = data_name, file = paste0("datsub", scenario, ".Rdata"))
}
run_subsets(data=dat, t=type=="AM", x=datsub1)

and with quote() and these also in combination with eval(), and many others.
Could somebody help me?

Thank you in advance,

Roberto

PS I really searched a lot in internet and Stackoverflow and found a lot which looked promising but in this function it needs something else.


Viewing all articles
Browse latest Browse all 204922

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>