[R] Return filematrix column by column names instead of column index?

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Wed Sep 9 02:00:10 CEST 2020


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 using 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 using 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.



More information about the R-help mailing list