equivalent of bash eval in R
1
0
Entering edit mode
7.5 years ago
Clare ▴ 160

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.6k views
ADD COMMENT
3
Entering edit mode
7.5 years ago
brentp 23k
print(dat[[locus]])
ADD COMMENT
0
Entering edit mode

Wow - thank you!! Thats perfect

ADD REPLY

Login before adding your answer.

Traffic: 1874 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6