[R] specifying column names in a vector of characters and the use?

Simon Blomberg s.blomberg1 at uq.edu.au
Mon Jul 19 04:18:47 CEST 2010


Try:

table.1[[hold[1]]]

Cheers,

Simon.


On 19/07/10 12:09, Seth wrote:
> Hi,
>
> What I would like to do is have a data.frame with column names and have
> these column names stored as strings in another vector.  Then I would like
> to be able to access the data.fram columns via referencing the vector of
> names.  The code below shows the last few executions that failed to retrieve
> the values for column named X1.  Seth
>
>
>    
>> table.1<-cbind(c(1,2,3,2,2),c(0,9,0,7,9),c(7,5,9,8,8))
>> table.1
>>      
>       [,1] [,2] [,3]
> [1,]    1    0    7
> [2,]    2    9    5
> [3,]    3    0    9
> [4,]    2    7    8
> [5,]    2    9    8
>    
>> table.1<-data.frame(table.1)
>> table.1
>>      
>    X1 X2 X3
> 1  1  0  7
> 2  2  9  5
> 3  3  0  9
> 4  2  7  8
> 5  2  9  8
>    
>> hold<-c("X1","X2","X3")
>> hold
>>      
> [1] "X1" "X2" "X3"
>    
>> table.1$X1
>>      
> [1] 1 2 3 2 2
>    
>> hold[1]
>>      
> [1] "X1"
>    
>> table.1$hold[1] # FROM HERE DOWN ARE MY ATTEMPTS TO ACCESS X1
>>      
> NULL
>    
>> table.1$(hold[1])
>>      
> Error: unexpected '(' in "table.1$("
>    
>> table.1$get(hold[1])
>>      
> Error: attempt to apply non-function
>    
>> table.1$(get(hold[1]))
>>      
> Error: unexpected '(' in "table.1$("
>    
>>      

-- 
Simon Blomberg, BSc (Hons), PhD, MAppStat.
Lecturer and Consultant Statistician
School of Biological Sciences
The University of Queensland
St. Lucia Queensland 4072
Australia
T: +61 7 3365 2506
email: S.Blomberg1_at_uq.edu.au
http://www.uq.edu.au/~uqsblomb/

Policies:
1.  I will NOT analyse your data for you.
2.  Your deadline is your problem

Statistics is the grammar of science - Karl Pearson.



More information about the R-help mailing list