[R] Turning Data Frame Columns into Vectors

Daniel Malter daniel at umd.edu
Sat Oct 15 23:22:15 CEST 2011


You access columns of a data.frame by column indices as in: X[ ,1], X[ ,2],
etc. The index before the comma would stand for the row if you wanted to
restrict those. The index after the comma captures the column.

That said, you typically would not "extract" rows from the data frame but
draw directly from the data frame for an analysis contingent on the factors
in column 1. You may want to look at the tapply() function, in particular. 

HTH,
Daniel


jawbonemurphy wrote:
> 
> Hi,
> 
> I converted an Excel file into a .txt file "X.txt" with no header (
> http://r.789695.n4.nabble.com/file/n3908157/X.txt X.txt ) and imported it
> with: 
> 
> X <- read.table("/Users/johnlogandurland/Desktop/X.txt", header=FALSE).
> 
> What I would like to do is to make the first column into a factors vector,
> and then analyze the rest of the columns in the data table using those
> factors.  The problem is, I haven't found a way to convert the rest of the
> columns into vectors with length = nrows so I can apply the factors vector
> to them.
> 
> When I try:
> 
> vect <- X[1]
> 
> I get an object with class "data.frame" and length 1, and the "[[]]"
> command seems to turn the column into a "factor" vector, though with the
> right length.  This seems like it should be a really easy thing to do, but
> I've looked through R-intro and been unable to find a good answer.  Any
> ideas?
> 
> Many Thanks,
> Logan
> 

--
View this message in context: http://r.789695.n4.nabble.com/Turning-Data-Frame-Columns-into-Vectors-tp3908157p3908298.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list