[R] Create subset using variable

David Winsemius dwinsemius at comcast.net
Sat Jan 21 23:59:21 CET 2012


On Jan 21, 2012, at 3:18 PM, pansophy wrote:

> I am trying to create a loop that will perform a series of  
> analyses.  I am
> using geeglm from geepack, which fails if there are any null values.
> Creating a subset solves this, but do not seem to be able to set the  
> subset
> dynamically based on a changing variable.

This is an exercise in guesswork since you have not provided the data  
structures that you are accessing.

>
> while (j <= y.num) {

If you want to avoid NULL values in sequences you can use seq_along()
>
>     strSubset = as.character(df.IV$IV[j])  #Gives column name in  
> quotes

Is there really a dataframe named 'df.IV' or are you perhaps an  
expatriate from another programming locale where the "." is an  
accessor operator?

>     df.data.sub = subset(df.data, strSubset>=0)
>
> #subset dataset is not created

`subset` uses nonstandard evaluation. It's very handy for interactive  
work but for programming you cannot use it in the manner you imagine.  
Try instead:

df.data.sub =df.data[ df[["IV"]] >= 0 )

Or perhaps:

df.data.sub =df.data[ df[[strSubset]] >= 0 )

Although I'm not sure what `strSubset` will evaluate to in your  
situation.


>
> # analyses on subset take place
>
>    j = j + 1
> }
>
> If I type the variable name in the formula it works, so I assume  
> that I am
> not creating the variable in a manner that allows it to be evaluated  
> in the
> subset function.

That much is certain.

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list