Strange behaviour with Cbind

#1
Hi,

I'm trying to reshape data and as part of this I have a data object containing a data frame that can have multiple pairs of XY columns and around 400 rows of numbers. I need to run a function for each pair of XY columns.

Eventually I'm looking to loop through the frame and extract each pair into a matrix.

Here's my question :

The statement below gives me a matrix but it's no good referring to the file with names as I won't know of these in advance.

xy <-cbind(data$PageViews.2014, data$Conversions.2014)

In readiness for the looping I'm trying to refer to the columns by column number :

xy <-cbind(data[1], data[2])

This statement gives me a Data Frame rather than an n*p matrix.

I ultimately need the pairs of XY columns in a n*p matrix. What's the best way of doing this? I'd rather do it in one stop per XY columns if possible.

Any suggestions would be gratefully received.
 

Dason

Ambassador to the humans
#4
That works perfectly. I didn't realise we could refer to frames like this. Thanks so much.
That's actually the way you should be grabbing columns from a dataframe. It's a subtle but important difference. When you grab dat[,1] you're grabbing the first column as a vector. If you use dat[1] you're actually grabbing it as a list/data.frame that has one element/column which is the vector you're interested in.