Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Addressing multiple columns of data frame by name

I have some data sets with high enough dimension (14) to be painful to plot pairwise in one go. In that case I'd like to be able to select a subset of the dataframe they're in, but I only know how to address columns by number. This is irritating and unclear when the code is read back:

partimat(MARKER ~ ., trim_data11[,c(1:5,NCOL(trim_data11))],method="qda")

What I would like to do is something like this, which doesn't work:

partimat(MARKER ~ ., trim_data11$(c(AF3,F7,P8,O1,O2,MARKER)),method="qda")

Is there a way to do this?

like image 761
bright-star Avatar asked Apr 26 '13 09:04

bright-star


1 Answers

You can address them with names as you suspect, just pass the names through as a character vector:

partimat(MARKER ~ ., trim_data11[, c("AF3","F7","P8","O1","O2","MARKER") ],method="qda")

As a simple example:

df <- data.frame( x = runif(5) , y =runif(5) , z = runif(5) )
df[,c("x","z")]
#         x         z
#1 0.5896444 0.1855764
#2 0.3486369 0.4936727
#3 0.1640928 0.1367027
#4 0.3167399 0.6686943
#5 0.7063566 0.6032699
like image 78
Simon O'Hanlon Avatar answered Oct 08 '22 06:10

Simon O'Hanlon