Does R have something like SAS macro variable?

I want to subset data frame by value of group variable, for example, the group variable has values a, b, c, d, plan to subset data like this:

subData<- OriginalData %>% filter(group=="a")

Created a piece of code to perform this procedure,
subsetting <- function(x) {
subData<- OriginalData %>% filter(group=="group") }

function(a) #call the function to perform subsetting
function(b)
function(c)
function(d)
Tried couple times, it doesn't work, the problem probably is from "filter(group="group")", I know SAS can assign a subsetting value by a macro variable, it works like this: when group="&group". don't know how to deal with it in R. Any help will be highly appreciated! Thank you!

Something like this?

library('tidyverse')
org_dat = tibble(dat = sample(LETTERS[1:4], 100, replace = TRUE))
subsetting = function(data, var){
  return( data %>% filter(dat == var) )
}
subsetting(data = org_dat, var = 'A')

But really, for such a simple function, I'd recommend just doing:

org_dat %>% filter(dat == 'A')

Which is basically what you are wrapping in your function.

Hope it helps!

4 Likes

Thank you!!!
By the way, the real function is much more complicated.

Ok, hope you can use the code as a point of reference to build your function on then :slightly_smiling_face:

How to create a new data frame (subset one) using your code? Thank you!

new_dat = subsetting(data = org_dat, var = 'A')
2 Likes

As you work to get a handle on R stuff, you might want to check out this thread:

Despite the opening topic description, there's quite a few resources there that are also appropriate for people who have some programming experience but in a very different language (e.g. SAS).

3 Likes

Please look at following code, always get 0 rows in the subset dataset, don't know what is wrong. Thank you!

library(dplyr)
df <- expand.grid(var = 1:10, grp = c("A", "B"))

df$grp<- as.character(df$grp)

subsetting= function(data, var) {
  return(data %>% filter(grp==var))
}

transit<- subsetting(data=df, var="A")

I suspect it has to do with using var to refer both to a column in the data frame as well as your parameter to filter from the grp field. This worked for me:

library(dplyr)
df <- expand.grid(var = 1:10, grp = c("A", "B"))

df$grp<- as.character(df$grp)

subsetting= function(data, grp_filter) {
  return(data %>% filter(grp==grp_filter))
}

transit<- subsetting(data=df, grp_filter="A")
transit
#>    var grp
#> 1    1   A
#> 2    2   A
#> 3    3   A
#> 4    4   A
#> 5    5   A
#> 6    6   A
#> 7    7   A
#> 8    8   A
#> 9    9   A
#> 10  10   A
3 Likes

Thank you!!! your code reminded me what's wrong in mine.

Based on your questions in this thread, I would highly recommend that you check out the free and super awesome R for Data Science book.

3 Likes

I prefer the tidyverse code that others have proposed, but if you want a raw R solution, the keyword "subset" will do the job and will work just as well in a function:

#Create a subset of a data.frame or data.table

subData <- subset(OriginalData, group == "a")

If you wish to make your own then I suggest this function:

makeSub <- function(data, groupVariable, value) {
return(subset(data, data[groupVariable,] == value))
}

In the function above, groupVariable = the number of the column you wish to use for subsetting.

You had no return statement in your function, BTW,

Rather than making a subSetting function, why not just use the built-in "subset" function? It's just as easy.

2 Likes

Thank you!
The reason is that the group has more than 30 values requested to be subset and the real function is more complicated, there are other procedure included in, planed to execute all procedure by one click.

OK. So the above function would be used as follows:

newData <- makeSub(OriginalData, ColumnNumber, "a")

1 Like

Please use proper code formatting @EuanGMason, it makes it a lot easier to understand what's going on :slightly_smiling_face:

```{r}
# Here be my gr8 code!
```