[R] dynamically extract data from a list

Dries Knapen dries.knapen at gmail.com
Tue Aug 12 03:36:19 CEST 2008


Hi,

Based on user input, I wrote a function that creates a list which  
looks like:

 > str(list)
List of 4
  $ varieties: chr [1:12] "temp.26_time.5dagen_biorep.1" "time. 
5dagen_temp.26_biorep.2" "temp.18_time.5dagen_biorep.1" "temp.18_time. 
5dagen_biorep.2" ...
  $ temp     : Factor w/ 2 levels "18","26": 2 2 1 1 2 2 1 1 1 1 ...
  $ time     : Factor w/ 3 levels "14dagen","28dagen",..: 3 3 3 3 1 1  
1 1 2 2 ...
  $ biorep   : Factor w/ 2 levels "1","2": 1 2 1 2 1 2 1 2 1 2 ...

Now, based on user input as well, I want to dynamically extract data  
from list$varieties. Therefore, I wrote a function which generates a  
string containing the data extraction conditions which looks like this:

 > query <- make.contrast.substring(negative.contrast, list)
Read 1 item
[1] "(list$temp=='18')&(list$time=='14dagen'|list$time=='28dagen'|list 
$time=='5dagen')&(list$biorep=='1'|list$biorep=='2')"

Now what I want to achieve is to extract data by doing:

list$varieties[query]

which doesn't work since "query" is a string and object names are not  
expanded...

Obviously, manually copying the string like so

list$varieties[(list$temp=='18')&(list$time=='14dagen'|list 
$time=='28dagen'|list$time=='5dagen')&(list$biorep=='1'|list 
$biorep=='2')]

works perfectly - but I need it to be automated.

I'm quite new to R and used to programming in PHP, so I may just be  
"conceptually" confused about how to do this. Any help would be  
greatly appreciated.

thanks in advance,
Dries Knapen



********************************************
Dr. Dries Knapen

University of Antwerp
Department of Biology
Ecophysiology, Biochemistry and Toxicology
Groenenborgerlaan 171 - U711, B-2020 Antwerp
Belgium

tel ++32 3 265 33 49
fax ++32 3 265 34 97



More information about the R-help mailing list