Is there a quick way to code for those specific vectors? Like I only want to use every 4th column in my matrix then plot the selected columns. I'm very new to R and have absolutely no idea what I'm doing. I know how to select a single vector and how to select a certain number in a row but that doesn't really help.
-
It always helps if you give an example of what you're trying to do, with a little bit of data supplied. Even if you only can start your code it gives us something to work with. See this for an example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – user14353 Oct 26 '16 at 03:12
-
This is for a microarray of gene expression for 24 samples (columns) & around 13000 genes (rows). I would like to make use the hexplom command to plot 6 of the 24 samples (not in order next to each other in the matrix) against each other to make comparisons. – Alysha Oct 26 '16 at 03:47
-
How would it work for just a set of random column numbers? Say I wanted to do columns 2, 7, 9, and 16 from a matrix with 20 columns. – Alysha Oct 26 '16 at 03:54
-
Ok so looking at hexplom you could do something like this. Say your dataset is called `foo`. To plot columns 2,7,9, and 16 you can do this: `hexplom(~foo[,c(2,7,9,16)])` – user14353 Oct 26 '16 at 04:07
-
That worked! I had tried that before but missed the ~ – Alysha Oct 26 '16 at 04:10
-
Thanks a bunch! I'm a biologist trying to learn coding and I have been riding the struggle bus. – Alysha Oct 26 '16 at 04:11
-
If you could select my answer that would be great. – user14353 Oct 26 '16 at 04:17
-
@user14353 how do I do that? – Alysha Oct 26 '16 at 04:19
-
There should be a checkmark by the arrows on my answer. Just click that. – user14353 Oct 26 '16 at 04:20
-
I think I got it. Thanks again! – Alysha Oct 26 '16 at 04:21
1 Answers
If you're looking to extract every 4th column from a matrix you can use seq()
.
Here's an example. I made a dummy dataset: foo<-matrix(c(rep(c(4,3,2,7),100)),nrow=10,ncol=10)
Then you can store the column indexes that you want from your matrix like so:
colsyouwant<-seq(from = 4, to = ncol(foo), by = 4)
from = whatever column you'd like to start from, in your case the 4th. Then you specify where you'd like to stop, so I used the ncol
function to count how many columns are in the matrix. In this case my matrix isn't a multiple of 4 but it doesn't matter because seq
stops before then. Then by=4 because you want to select every fourth column.
The colsyouwant
now equals to 4 8
. Simply use brackets and the name of your variable to get the columns you want out. foo[,colsyouwant]
. Here the brackets just specify what part of the matrix I want as an output, it goes [rows,columns]. Since I want all the rows I leave that spot blank and then specify the rows using the colsyouwant
variable, or in other words 4 8
.

- 135
- 5
-
How would it work for just a set of random column numbers? Say I wanted to do columns 2, 7, 9, and 16 from a matrix with 20 columns. – Alysha Oct 26 '16 at 03:55