s-news
[Top] [All Lists]

Re: using get() to extract a vector from a data frame

To: "Jeff P. Hollenbeck" <Jeff.Hollenbeck@oregonstate.edu>, s-news@lists.biostat.wustl.edu
Subject: Re: using get() to extract a vector from a data frame
From: "Greg Snow" <Greg.Snow@imail.org>
Date: Thu, 13 Mar 2008 15:29:31 -0600
In-reply-to: <001b01c88557$debc4af0$1200a8c0@jackstraw>
References: <005a01c88471$6895ae70$1200a8c0@jackstraw> <001b01c88557$debc4af0$1200a8c0@jackstraw>
Thread-index: AciFT9wNXPeQjen8S5qQevy1C/eD1wAAUUZw
Thread-topic: [S] using get() to extract a vector from a data frame
Try:
 
> y <- get(data)[[yvar]]
 
The '$' is a special case of '[[]]' which treats yvar in a special way, you don't want the special treatment, so just use [[]] and it should work.
 

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow@imail.org
(801) 408-8111

 


From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Jeff P. Hollenbeck
Sent: Thursday, March 13, 2008 4:16 PM
To: s-news@lists.biostat.wustl.edu
Subject: [S] using get() to extract a vector from a data frame

This is very similar to my previous post on this problem, but I have pinned down my issue.  Perhaps someone on this list would be kind enough to help me solve this.
 
I am trying to extract a vector (column) from a data frame using the code below.  I first define the data frame and target vector (column) names as variables.  Then I attempt to use get() to extract the target vector.  That is where I am having difficulty.  Here is the code
 
#
# Get response variable name components.
data <- as.character(multinom.model@call[[3]])
yvar <- as.character(multinom.model@formula[[2]])
#
# Extract response (y) vector.
y <- get(data)$yvar
#
 
The problem is the way yvar gets treated in the get statement.  I know this because if I manually replace yvar with a valid column name, I get the correct behavior.  I simply have no idea how to get yvar to be treated correctly in the get() statement.   As it is now, I get NULL result for y.  Any help here is much appreciated.
 
Thank you very much,
 
jeff.
 
Jeff P. Hollenbeck
Dept. Forest Resources
Oregon State University
Corvallis OR 97330
Email: Jeff.Hollenbeck@oregonstate.edu
Phone: 541-737-5500
<Prev in Thread] Current Thread [Next in Thread>