Question: equivalent of bash eval in R
0
gravatar for Clare
5.1 years ago by
Clare120
United States
Clare120 wrote:

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.2k views
ADD COMMENTlink written 5.1 years ago by Clare120
2
gravatar for brentp
5.1 years ago by
brentp23k
Salt Lake City, UT
brentp23k wrote:

    print(dat[[locus]])

ADD COMMENTlink written 5.1 years ago by brentp23k

Wow - thank you!! Thats perfect

ADD REPLYlink written 5.1 years ago by Clare120
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1554 users visited in the last hour