This is a test version of Biostars. For the public version, visit https://www.biostars.org.
equivalent of bash eval in R

I have a newbie R question. So my data are Loci names and allele frequencies. The loci and associated allele frequences I have as a list of vectors called dat. I also have another list 'locusnames' with just the locus names. See below - but note this is a subset I'm working with of just a few loci and alleles - ultimately just 15 loci, and perhaps 5-20 alleles per locus.

> dat
$D3S135  ### This is the locus name
[1] "0.1" "0.3" "0.4" "0.2"  ### These are the allele frequencies

$vWA
[1] "0.05" "0.6"  "0.3"  "0.05"

$D8S117
[1] "0.3"  "0.3"  "0.1"  "0.2"  "0.05" "0.05"

> locusnames

[1] "D3S135" "vWA"    "D8S117"

I want to loop through the locusnames list, and access the allele frequencies from dat for that locus - so I can feed the allele frequencies for each locus into a function. But R returns NULL

> for(locus in locusnames) {
+         print(dat$locus)
+ }
NULL
NULL
NULL

I was expecting the allele frequencies to be printed for each locus.

I think this is because R is not interpreting dat$locus as dat$D3S135 or dat$vWA ... In bash I'd use eval for this situation. Is there anything in R to do this? Or another way to go about this?

loop r

1 answer

print(dat[[locus]])

Wow - thank you!! Thats perfect

Log in to answer this question.