evaluating a character string as a variable name in an ExpressionSet object
2
0
Entering edit mode
@martin-morgan-1513
Last seen 18 days ago
United States
Hi Kavitha -- Kavitha Venkatesan <kavitha.venkatesan at="" gmail.com=""> writes: > I would like to know how to evaluate a character string as a variable name > in R. Specifically, I need to "compute" the variable name in the phenoData > slot of an ExpressionSet object. > >> varLabels(sample.ExpressionSet) > [1] "sex" "type" "score" > > Now I need to retrieve the factors sample.ExpressionSet$sex, > sample.ExpressionSet$type and sample.ExpressionSet$score in turn. Use [[ rather than $. > data(sample.ExpressionSet) > sample.ExpressionSet[["sex"]] [1] Female Male Male Male Female Male Male Male Female Male [11] Male Female Male Male Female Female Female Male Male Female [21] Male Female Male Male Female Female Levels: Female Male e.g., > for (lbl in varLabels(sample.ExpressionSet)) + print(sample.ExpressionSet[[lbl]]) [1] Female Male Male Male Female Male Male Male Female Male [11] Male Female Male Male Female Female Female Male Male Female [21] Male Female Male Male Female Female Levels: Female Male [1] Control Case Control Case Case Control Case Case Case [10] Control Case Control Case Case Case Control Case Control [19] Case Case Control Control Control Control Case Case Levels: Case Control [1] 0.75 0.40 0.73 0.42 0.93 0.22 0.96 0.79 0.37 0.63 0.26 0.36 0.41 0.80 0.10 [16] 0.41 0.16 0.72 0.17 0.74 0.35 0.77 0.27 0.98 0.94 0.32 Martin > Furthermore, I need to be able to generalize my code so that I can retrieve > this factor for any phenodata variable in any given ExpressionSet object. > > Clearly I cannot do something like > > for (i in c(1:length(varLabels(sample.ExpressionSet))) > { > class_label <- varLabels(sample.ExpresionSet)[i] > class_label_factor <- sample.ExpressionSet$class_label > } > > Here class_label is interpreted as a character string by R, but I need for > it to be evaluated as a variable name. Seems straightforward, but I couldn't > figure it out...How can I do this? Thanks in advance for any help! > > Kavitha > > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor -- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
Cancer Cancer • 878 views
ADD COMMENT
0
Entering edit mode
@martin-morgan-1513
Last seen 18 days ago
United States
Kavitha Venkatesan <kavitha.venkatesan at="" gmail.com=""> writes: > Thanks for all your suggestions. Both Martin's suggestions below (simpler > solution) and Saroj's approach worked. > FYI- Saroj's approach which when adapted to my situation would entail the > following code: >>for(i in varLabels(sample.ExpressionSet)) >> print( eval(parse(text=paste("sample.ExpressionSet","$",i,sep=""))) ) one would not normally do this; it is like making coffee from the words 'coffee beans' rather than from the coffee beans themselves. Martin > Kavitha > > > On Thu, Apr 16, 2009 at 11:41 PM, Martin Morgan <mtmorgan at="" fhcrc.org=""> wrote: > > Hi Kavitha -- > > Kavitha Venkatesan <kavitha.venkatesan at="" gmail.com=""> writes: > > I would like to know how to evaluate a character string as a > variable name > in R. Specifically, I need to "compute" the > variable name in the phenoData > slot of an ExpressionSet > object. > >> varLabels(sample.ExpressionSet) > [1] "sex" > ? "type" ?"score" > > Now I need to retrieve the factors > sample.ExpressionSet$sex, > sample.ExpressionSet$type and > sample.ExpressionSet$score in turn. > > > > Use [[ rather than $. > > data(sample.ExpressionSet) > sample.ExpressionSet[["sex"]] ?[1] > Female Male ? Male ? Male ? Female Male ? Male ? Male ? Female Male [11] > Male ? Female Male ? Male ? Female Female Female Male ? Male ? > Female [21] Male ? Female Male ? Male ? Female Female Levels: > Female Male > e.g., > > for (lbl in varLabels(sample.ExpressionSet)) + ? ? > print(sample.ExpressionSet[[lbl]]) ?[1] Female Male ? Male ? > Male ? Female Male ? Male ? Male ? Female Male [11] Male ? > Female Male ? Male ? Female Female Female Male ? Male ? Female [21] > Male ? Female Male ? Male ? Female Female Levels: Female Male ?[1] > Control Case ? ?Control Case ? ?Case ? ?Control Case ? ?Case ? ?Case [10] > Control Case ? ?Control Case ? ?Case ? ?Case ? ?Control Case ? > ?Control [19] Case ? ?Case ? ?Control Control Control Control > Case ? ?Case Levels: Case Control ?[1] 0.75 0.40 0.73 0.42 > 0.93 0.22 0.96 0.79 0.37 0.63 0.26 0.36 0.41 0.80 0.10 [16] 0.41 > 0.16 0.72 0.17 0.74 0.35 0.77 0.27 0.98 0.94 0.32 > > Martin > > > Furthermore, I need to be able to generalize my code so > that I can retrieve > this factor for any phenodata variable in > any given ExpressionSet object. > > Clearly I cannot do > something like > > for (i in > c(1:length(varLabels(sample.ExpressionSet))) > { > > class_label <- varLabels(sample.ExpresionSet)[i] > > class_label_factor <- ?sample.ExpressionSet$class_label > } > > > Here class_label is interpreted as a character string by R, but I > need for > it to be evaluated as a variable name. Seems > straightforward, but I couldn't > figure it out...How can I do > this? Thanks in advance for any help! > > Kavitha > > > > ? ? ? [[alternative HTML version deleted]] > > > _______________________________________________ > Bioconductor > mailing list > Bioconductor at stat.math.ethz.ch > > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the > archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > -- > > > Martin Morgan Computational Biology / Fred Hutchinson Cancer > Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, > WA 98109 > Location: Arnold Building M1 B861 Phone: (206) 667-2793 > > > -- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
ADD COMMENT
0
Entering edit mode
> > > Thanks for all your suggestions. Both Martin's suggestions below (simpler > > solution) and Saroj's approach worked. > > FYI- Saroj's approach which when adapted to my situation would entail the > > following code: > >>for(i in varLabels(sample.ExpressionSet)) > >> print( eval(parse(text=paste("sample.ExpressionSet","$",i,sep=""))) ) > > one would not normally do this; it is like making coffee from the > words 'coffee beans' rather than from the coffee beans themselves. > I completely agree. And like the analogy you presented :=) I merely mentioned this in response to Saroj's suggestion for awareness of the existence of the "eval" function in general. Kavitha > > Martin > > > Kavitha > > > > > > On Thu, Apr 16, 2009 at 11:41 PM, Martin Morgan <mtmorgan@fhcrc.org> > wrote: > > > > Hi Kavitha -- > > > > Kavitha Venkatesan <kavitha.venkatesan@gmail.com> writes: > > > I would like to know how to evaluate a character string as a > > variable name > in R. Specifically, I need to "compute" the > > variable name in the phenoData > slot of an ExpressionSet > > object. > >> varLabels(sample.ExpressionSet) > [1] "sex" > > "type" "score" > > Now I need to retrieve the factors > > sample.ExpressionSet$sex, > sample.ExpressionSet$type and > > sample.ExpressionSet$score in turn. > > > > > > > > Use [[ rather than $. > > > data(sample.ExpressionSet) > sample.ExpressionSet[["sex"]] > [1] > > Female Male Male Male Female Male Male Male Female Male > [11] > > Male Female Male Male Female Female Female Male Male > > Female [21] Male Female Male Male Female Female > Levels: > > Female Male > > e.g., > > > for (lbl in varLabels(sample.ExpressionSet)) + > > print(sample.ExpressionSet[[lbl]]) [1] Female Male Male > > Male Female Male Male Male Female Male [11] Male > > Female Male Male Female Female Female Male Male Female > [21] > > Male Female Male Male Female Female Levels: Female Male > [1] > > Control Case Control Case Case Control Case Case Case > [10] > > Control Case Control Case Case Case Control Case > > Control [19] Case Case Control Control Control Control > > Case Case Levels: Case Control [1] 0.75 0.40 0.73 0.42 > > 0.93 0.22 0.96 0.79 0.37 0.63 0.26 0.36 0.41 0.80 0.10 [16] 0.41 > > 0.16 0.72 0.17 0.74 0.35 0.77 0.27 0.98 0.94 0.32 > > > > Martin > > > > > Furthermore, I need to be able to generalize my code so > > that I can retrieve > this factor for any phenodata variable in > > any given ExpressionSet object. > > Clearly I cannot do > > something like > > for (i in > > c(1:length(varLabels(sample.ExpressionSet))) > { > > > class_label <- varLabels(sample.ExpresionSet)[i] > > > class_label_factor <- sample.ExpressionSet$class_label > } > > > > > Here class_label is interpreted as a character string by R, but I > > need for > it to be evaluated as a variable name. Seems > > straightforward, but I couldn't > figure it out...How can I do > > this? Thanks in advance for any help! > > Kavitha > > > > > > [[alternative HTML version deleted]] > > > > _______________________________________________ > Bioconductor > > mailing list > Bioconductor@stat.math.ethz.ch > > > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the > > archives: > > http://news.gmane.org/gmane.science.biology.informatics.conductor > > -- > > > > > > Martin Morgan Computational Biology / Fred Hutchinson Cancer > > Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, > > WA 98109 > > Location: Arnold Building M1 B861 Phone: (206) 667-2793 > > > > > > > > -- > Martin Morgan > Computational Biology / Fred Hutchinson Cancer Research Center > 1100 Fairview Ave. N. > PO Box 19024 Seattle, WA 98109 > > Location: Arnold Building M1 B861 > Phone: (206) 667-2793 > [[alternative HTML version deleted]]
ADD REPLY
0
Entering edit mode
@kavitha-venkatesan-3399
Last seen 7.5 years ago
United States
Thanks for all your suggestions. Both Martin's suggestions below (simpler solution) and Saroj's approach worked. FYI- Saroj's approach which when adapted to my situation would entail the following code: >for(i in varLabels(sample.ExpressionSet)) > print( eval(parse(text=paste("sample.ExpressionSet","$",i,sep=""))) ) Kavitha On Thu, Apr 16, 2009 at 11:41 PM, Martin Morgan <mtmorgan@fhcrc.org> wrote: > Hi Kavitha -- > > Kavitha Venkatesan <kavitha.venkatesan@gmail.com> writes: > > > I would like to know how to evaluate a character string as a variable > name > > in R. Specifically, I need to "compute" the variable name in the > phenoData > > slot of an ExpressionSet object. > > > >> varLabels(sample.ExpressionSet) > > [1] "sex" "type" "score" > > > > Now I need to retrieve the factors sample.ExpressionSet$sex, > > sample.ExpressionSet$type and sample.ExpressionSet$score in turn. > > Use [[ rather than $. > > > data(sample.ExpressionSet) > > sample.ExpressionSet[["sex"]] > [1] Female Male Male Male Female Male Male Male Female Male > [11] Male Female Male Male Female Female Female Male Male Female > [21] Male Female Male Male Female Female > Levels: Female Male > > e.g., > > > for (lbl in varLabels(sample.ExpressionSet)) > + print(sample.ExpressionSet[[lbl]]) > [1] Female Male Male Male Female Male Male Male Female Male > [11] Male Female Male Male Female Female Female Male Male Female > [21] Male Female Male Male Female Female > Levels: Female Male > [1] Control Case Control Case Case Control Case Case Case > [10] Control Case Control Case Case Case Control Case > Control > [19] Case Case Control Control Control Control Case Case > Levels: Case Control > [1] 0.75 0.40 0.73 0.42 0.93 0.22 0.96 0.79 0.37 0.63 0.26 0.36 0.41 0.80 > 0.10 > [16] 0.41 0.16 0.72 0.17 0.74 0.35 0.77 0.27 0.98 0.94 0.32 > > > Martin > > > Furthermore, I need to be able to generalize my code so that I can > retrieve > > this factor for any phenodata variable in any given ExpressionSet object. > > > > Clearly I cannot do something like > > > > for (i in c(1:length(varLabels(sample.ExpressionSet))) > > { > > class_label <- varLabels(sample.ExpresionSet)[i] > > class_label_factor <- sample.ExpressionSet$class_label > > } > > > > Here class_label is interpreted as a character string by R, but I need > for > > it to be evaluated as a variable name. Seems straightforward, but I > couldn't > > figure it out...How can I do this? Thanks in advance for any help! > > > > Kavitha > > > > [[alternative HTML version deleted]] > > > > _______________________________________________ > > Bioconductor mailing list > > Bioconductor@stat.math.ethz.ch > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > > -- > Martin Morgan > Computational Biology / Fred Hutchinson Cancer Research Center > 1100 Fairview Ave. N. > PO Box 19024 Seattle, WA 98109 > > Location: Arnold Building M1 B861 > Phone: (206) 667-2793 > [[alternative HTML version deleted]]
ADD COMMENT

Login before adding your answer.

Traffic: 235 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