Benjamin Ward (ENV)
2013-Jan-25 23:17 UTC
[R] Removal of columns from matrix where all values of the column are identical.
Hi all, I'd like to write a piece of code which will remove columns from a matrix, if the column contains only one value, say every value in the column is a "3": Matrix <- matrix(NA, nrow=5, ncol=4) Matrix[,1] <- c(1,2,3,4,5) Matrix[,2] <- c(3,3,3,3,3) Matrix[,3] <- c(5,4,3,2,1) Matrix[,4] <- c(5,1,4,3,2) [,1] [,2] [,3] [,4] [1,] 1 3 5 5 [2,] 2 3 4 1 [3,] 3 3 3 4 [4,] 4 3 2 3 [5,] 5 3 1 2 What I have written so far is a loop which will see if all values are the same, a bit of a hack since it just checks all values are equal to the first value of the column, if not, by definition the column cannot contain only one value/variable/character: removals<-c() for(i in 1:ncol(Matrix)){ if(all(Matrix[,i] == Matrix[[1,i]])){ removals<-append(removals, i) } } new.Matrix <- Matrix[,-removals] This works for matrices with numbers or characters. My question is - is there a better or more efficient way of doing this, maybe with apply or something. My first thought was apply set to operate over all columns, but was unsure of the indexing and selecting columns to be deleted. Thanks, Ben W. University of East Anglia (ENV): b.ward@uea.ac.uk The Sainsbury Laboratory: ben.ward@sainsbury-laboratory.ac.uk [[alternative HTML version deleted]]
arun
2013-Jan-26 02:28 UTC
[R] Removal of columns from matrix where all values of the column are identical.
Hi, May be this helps: Matrix[,colSums(diff(Matrix))!=0] #???? [,1] [,2] [,3] #[1,]??? 1??? 5??? 5 #[2,]??? 2??? 4??? 1 #[3,]??? 3??? 3??? 4 #[4,]??? 4??? 2??? 3 #[5,]??? 5??? 1??? 2 A.K. ----- Original Message ----- From: Benjamin Ward (ENV) <B.Ward at uea.ac.uk> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Friday, January 25, 2013 6:17 PM Subject: [R] Removal of columns from matrix where all values of the column are identical. Hi all, I'd like to write a piece of code which will remove columns from a matrix, if the column contains only one value, say every value in the column is a "3": Matrix <- matrix(NA, nrow=5, ncol=4) Matrix[,1] <- c(1,2,3,4,5) Matrix[,2] <- c(3,3,3,3,3) Matrix[,3] <- c(5,4,3,2,1) Matrix[,4] <- c(5,1,4,3,2) ? ? ? [,1] [,2] [,3] [,4] [1,]? ? 1? ? 3? ? 5? ? 5 [2,]? ? 2? ? 3? ? 4? ? 1 [3,]? ? 3? ? 3? ? 3? ? 4 [4,]? ? 4? ? 3? ? 2? ? 3 [5,]? ? 5? ? 3? ? 1? ? 2 What I have written so far is a loop which will see if all values are the same, a bit of a hack since it just checks all values are equal to the first value of the column, if not, by definition the column cannot contain only one value/variable/character: removals<-c() for(i in 1:ncol(Matrix)){ ? if(all(Matrix[,i] == Matrix[[1,i]])){ ? ? removals<-append(removals, i) ? } } new.Matrix <- Matrix[,-removals] This works for matrices with numbers or characters. My question is - is there a better or more efficient way of doing this, maybe with apply or something. My first thought was apply set to operate over all columns, but was unsure of the indexing and selecting columns to be deleted. Thanks, Ben W. University of East Anglia (ENV): b.ward at uea.ac.uk The Sainsbury Laboratory: ben.ward at sainsbury-laboratory.ac.uk ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
arun
2013-Jan-26 02:34 UTC
[R] Removal of columns from matrix where all values of the column are identical.
Hi, I guess this should also work: ?Matrix[,apply(Matrix,2,function(x) all(c(TRUE,x[-length(x)]!=x[-1])))] #???? [,1] [,2] [,3] #[1,]??? 1??? 5??? 5 #[2,]??? 2??? 4??? 1 #[3,]??? 3??? 3??? 4 #[4,]??? 4??? 2??? 3 #[5,]??? 5??? 1??? 2 A.K. ----- Original Message ----- From: Benjamin Ward (ENV) <B.Ward at uea.ac.uk> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Friday, January 25, 2013 6:17 PM Subject: [R] Removal of columns from matrix where all values of the column are identical. Hi all, I'd like to write a piece of code which will remove columns from a matrix, if the column contains only one value, say every value in the column is a "3": Matrix <- matrix(NA, nrow=5, ncol=4) Matrix[,1] <- c(1,2,3,4,5) Matrix[,2] <- c(3,3,3,3,3) Matrix[,3] <- c(5,4,3,2,1) Matrix[,4] <- c(5,1,4,3,2) ? ? ? [,1] [,2] [,3] [,4] [1,]? ? 1? ? 3? ? 5? ? 5 [2,]? ? 2? ? 3? ? 4? ? 1 [3,]? ? 3? ? 3? ? 3? ? 4 [4,]? ? 4? ? 3? ? 2? ? 3 [5,]? ? 5? ? 3? ? 1? ? 2 What I have written so far is a loop which will see if all values are the same, a bit of a hack since it just checks all values are equal to the first value of the column, if not, by definition the column cannot contain only one value/variable/character: removals<-c() for(i in 1:ncol(Matrix)){ ? if(all(Matrix[,i] == Matrix[[1,i]])){ ? ? removals<-append(removals, i) ? } } new.Matrix <- Matrix[,-removals] This works for matrices with numbers or characters. My question is - is there a better or more efficient way of doing this, maybe with apply or something. My first thought was apply set to operate over all columns, but was unsure of the indexing and selecting columns to be deleted. Thanks, Ben W. University of East Anglia (ENV): b.ward at uea.ac.uk The Sainsbury Laboratory: ben.ward at sainsbury-laboratory.ac.uk ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
Ben Bolker
2013-Jan-26 02:38 UTC
[R] Removal of columns from matrix where all values of the column are identical.
Benjamin Ward (ENV <B.Ward <at> uea.ac.uk> writes:> I'd like to write a piece of code which will remove columns from a matrix, ifthe column contains only one> value, say every value in the column is a "3": > > Matrix <- matrix(NA, nrow=5, ncol=4) > Matrix[,1] <- c(1,2,3,4,5) > Matrix[,2] <- c(3,3,3,3,3) > Matrix[,3] <- c(5,4,3,2,1) > Matrix[,4] <- c(5,1,4,3,2) > > [,1] [,2] [,3] [,4] > [1,] 1 3 5 5 > [2,] 2 3 4 1 > [3,] 3 3 3 4 > [4,] 4 3 2 3 > [5,] 5 3 1 2I think you want Matrix[, apply(Matrix,2,function(x) { length(unique(x))>1 ) } ] The anonymous function tests whether there is more than one unique value. apply() runs it on every column and returns a logical vector. The [ , ] indexing selects the corresponding columns ...