Return filematrix column by column names instead of column index?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Return filematrix column by column names instead of column index?

Nevil Amos
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Return filematrix column by column names instead of column index?

Jim Lemon-4
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.