Skip to content

Separating variables in read.table

4 messages · Richardson, Patrick, Jorge Ivan Velez, Etienne Bellemare Racine +1 more

#
If I have a table (we'll call it, "test") containing two columns (as below):

i	x1	x2	x3	x4	x5	y
0	1.125	0.232	7.160	0.0859 8.905 1.5563
7	0.920	0.268	8.804	0.0865 7.388 0.8976
15	0.835	0.271	8.108	0.0852 5.348 0.7482
22	1.000	0.237	6.370	0.0838 8.056 0.7160
29	1.150	0.192	6.441	0.0821 6.960 0.3130
37	0.990	0.202	5.154	0.0792 5.690 0.3617
44	0.840	0.184	5.896	0.0812 6.932 0.1139
58	0.650	0.200	5.336	0.0806 5.400 0.1139


Is there a simple command to break this table into individual variables without having to code:

i <- test$i
x1 <- test$x1
x2 <- test$x2
.
.
.
And so on. . 

Many Thank for any assistance.

Patrick
This email message, including any attachments, is for th...{{dropped:6}}
#
Patrick -
    There's no simple way to do what you want, because
R discourages you from having lots of separate
related objects.  Instead, you are encouraged to store
your objects in an organized form, such as a list, 
data frame  or matrix.  For your example, I'm assuming 
you are using the word "table" to describe a data.frame. 
If this is the case , you can refer to the individual
columns of test as

test[,1]
test[,2]
   etc.

or

test$x1
test$x2
   etc.

or

test[,'x1']
test[,'x2']

Also remember that R has functions that can operate on
each row or column of a matrix or data frame.  So if you 
wanted the means of each column of test, you could write

     apply(test,2,mean)
                                        - Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu
On Fri, 17 Apr 2009, Richardson, Patrick wrote: