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

David Winsemius dwinsemius at comcast.net
Mon Jul 19 04:40:58 CEST 2010


On Jul 18, 2010, at 10:09 PM, 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

Try instead:

table.1[ , hold[1] ]

The "$" formalism does not evaluate its argument, but the "[" function  
does.

-- 
David.
>> 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$("
>>
> -- 
> View this message in context: http://r.789695.n4.nabble.com/specifying-column-names-in-a-vector-of-characters-and-the-use-tp2293494p2293494.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list