[R] Create subset using variable

Ken katakagi at bu.edu
Sun Jan 22 00:01:46 CET 2012


pansophy <mjs2134 <at> columbia.edu> writes:

> 
> 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.  
> 
> while (j <= y.num) {
> 
>      strSubset = as.character(df.IV$IV[j])  #Gives column name in quotes
>      df.data.sub = subset(df.data, strSubset>=0)
> 
> #subset dataset is not created
> 
>  # 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.  Any help would be greatly appreciated!
> 
> Michael 
> 
> --
> View this message in context:
http://r.789695.n4.nabble.com/Create-subset-using-variable-tp4316812p4316812.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> 

I think you want to try and use the double bracket with your df to access the
column of interest in your data frame.  To remove null values, you could use the
na.omit() function, assuming when you say null values are represented as NAs:

while (j <= y.num) {
 
      strSubset = as.character(df.IV$IV[j])  #Gives column name in quotes
      df.data.sub = df.data[[strSubset]]
      df.data.sub = na.omit(df.data.sub) # if null values are given as NA
      df.data.sub = df.data.sub[df.data.sub >= 0] # if null values are < 0

 #subset dataset is not created
 
  # analyses on subset take place
 
     j = j + 1
  }

Hope that helps,
Ken



More information about the R-help mailing list