0

I am trying to create a function that plots graphs for either an entire dataset, or a subset of the data. The function needs to be able to do both so that you can plot the subset if you so wish. I am struggling with just coming up with the generic subset function.

I currently have this code (I am more of a SAS user so R is confusing me a bit):

subset<-function(dat, varname, val)
if(dat$varname==val) {
    data<-subset(dat, dat$varname==val) 
}

But R keeps returning this error message:

Error in if (dat$varname == val) { : argument is of length zero

Could someone help me to resolve this? Thanks so much! I figure it may have to do with the way I wrote it.

1
  • 1
    Please provide complete code including the test code that generates the error and all inputs so that anyone else can reproduce the problem on their own machine. Commented Nov 29, 2015 at 18:08

1 Answer 1

2

First off all the $ operator can not handle variables. In your code you are always looking up a column named varname. Replace $varname with [varname] instead. The next error is that you are conditioning on a vector, dat$varname==val will be vector of booleans. A third error in your code is that you are naming your function subset and thus overlayering the subset function in the base package. So the inner call to subset will be a recursive call to your own function. To fix this rename your function or you have to specify that it is the subset function in the base package you are calling with base::subset(dat, dat[varname]==val). The final error in the code is that your function does not return anything. Do not assign the result to the variable data but return it instead.

Here is how the code should look like.

mySubset<-function(dat, varname, val)
if(any(dat[varname]==val)) {
    subset(dat, dat[varname]==val) 
} else {
    NA
}

Or even better

mySubset <- function(dat,varname,val) dat[dat[varname] == val]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks so much for your response. I realized the issues with the dat$varname in my code, but when I input it the way you indicated, it says the variable is not found. So, it's still not reading it in as dat$varname
Oh I also forgot that what I am trying to return is an assignment of the subsetted data as the new dataset we are working with. Thus, data<-subset(dat, dat[varname]==val)
First of all varname should be a string so if your column is called x you must call the function with "x" and not x. It is much better to do the assignment outside the function use data <- mySubset(data,varname,val)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.