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

2010-07-18 Thread Seth

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,]107
[2,]295
[3,]309
[4,]278
[5,]298
 
 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$(
 
-- 
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@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.


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

2010-07-18 Thread Erik Iverson

Hello,


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,]107
[2,]295
[3,]309
[4,]278
[5,]298


See easier way to construct your object below.

snip

[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$(


You need to index with ?[

Here is a complete example:

table.1- data.frame(X1 = c(1,2,3,2,2),
 X2 = c(0,9,0,7,9),
 X3 = c(7,5,9,8,8))

hold - names(table.1)

## to return a data.frame
table.1[hold[1]]

## to return a vector
table.1[[hold[1]]]

__
R-help@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.


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

2010-07-18 Thread Simon Blomberg

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,]107
[2,]295
[3,]309
[4,]278
[5,]298
   

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.

__
R-help@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.


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

2010-07-18 Thread David Winsemius


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,]107
[2,]295
[3,]309
[4,]278
[5,]298


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@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.


__
R-help@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.