nevil amos
2020-Sep-08 11:37 UTC
[R] Return filematrix column by column names instead of column index?
Is there a way to get columns out of a filematrix using the column name
directly in the same way that you can with a regular matrix?
library(filematrix)
M<-t(matrix(1:3,3,4))
colnames(M)<-c("one","two", "three")
M
#Extract column
M[,1]
M[,"one"]
M[,c(1,3)]
M[,c("one","three")]
FM<-fm.create.from.matrix(filenamebase = "test",mat = M)
FM[,1]
colnames(FM)
#extract by column by name does not work
FM[,"one"]
#workaround using grep
#is there a more direct way of doing this to retrieve more than one column?
FM[,grep("one",colnames(FM))]
FM[,c(grep("one",colnames(FM)),grep("three",colnames(FM)))]
many thanks for any suggestions
Nevil Amos
[[alternative HTML version deleted]]
Jim Lemon
2020-Sep-09 00:00 UTC
[R] Return filematrix column by column names instead of column index?
Hi Nevil,
As I don't have the filematrix package this is really an "any
suggestion":
FM[,which(colnames(FM) %in% c("one","three"))]
Jim
On Tue, Sep 8, 2020 at 9:43 PM nevil amos <nevil.amos at gmail.com>
wrote:>
> Is there a way to get columns out of a filematrix using the column name
> directly in the same way that you can with a regular matrix?
>
> library(filematrix)
> M<-t(matrix(1:3,3,4))
> colnames(M)<-c("one","two", "three")
> M
> #Extract column
> M[,1]
> M[,"one"]
> M[,c(1,3)]
> M[,c("one","three")]
> FM<-fm.create.from.matrix(filenamebase = "test",mat = M)
> FM[,1]
> colnames(FM)
> #extract by column by name does not work
> FM[,"one"]
>
> #workaround using grep
> #is there a more direct way of doing this to retrieve more than one
column?
> FM[,grep("one",colnames(FM))]
>
>
FM[,c(grep("one",colnames(FM)),grep("three",colnames(FM)))]
>
> many thanks for any suggestions
>
> Nevil Amos
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.