HI Ben,
Sorry, I didn't test it at that time to different cases.
testm[,apply(testm,2,function(x) all(c(TRUE,x[-length(x)]!=x[-1])))]
x[-length(x)] #removes the last observation in each column
x[-1] #removes the first observation in each column
#and compares them for each column.
If I change the code a bit, it should work:
testm[,apply(testm,2,function(x) any(c(FALSE,x[-length(x)]!=x[-1])))]
# [,1] [,2] [,3] [,4]
#[1,] 1 3 5 1
#[2,] 2 3 4 2
#[3,] 3 3 3 3
#[4,] 4 4 2 4
#[5,] 5 3 1 4
#Changing your dataset:
testm1<-matrix(nrow=5, ncol=5)
testm1[,1] <- c(1,2,3,4,5)
testm1[,2] <- c(3,3,3,3,3)
testm1[,3] <- c(3,3,4,4,3)
testm1[,4] <- c(5,4,3,2,1)
testm1[,5] <- c(2,2,3,4,4)
testm1[,apply(testm1,2,function(x) any(c(FALSE,x[-length(x)]!=x[-1])))]
# [,1] [,2] [,3] [,4]
#[1,] 1 3 5 2
#[2,] 2 3 4 2
#[3,] 3 4 3 3
#[4,] 4 4 2 4
#[5,] 5 3 1 4
set.seed(15)
testm2<-matrix(c(sample(letters[1:5],20,replace=TRUE),rep("a",5)),ncol=5)
testm2[,apply(testm2,2,function(x) any(c(FALSE,x[-length(x)]!=x[-1])))]
# [,1] [,2] [,3] [,4]
#[1,] "d" "e" "a" "e"
#[2,] "a" "e" "d" "c"
#[3,] "e" "b" "c" "e"
#[4,] "d" "d" "d" "a"
#[5,] "b" "e" "e" "d"
A.K.
----- Original Message -----
From: Benjamin Ward (ENV) <[email protected]>
To: arun <[email protected]>
Cc: R help <[email protected]>
Sent: Saturday, January 26, 2013 8:37 AM
Subject: RE: [R] Removal of columns from matrix where all values of the column
are identical.
Hi,
I've been trying to work this out how it works, I'm still not totally sure but
seems to me it subsets the matrix according to whether each column returns all
TRUE, to not being the same values when you compare the column[-1] and the
column[-length(column)], essentially siding the column against itself and
making comparison? It seems that it also removed columns with any repeated
values, rather than columns in which all values are the same:
testm<-matrix(nrow=5, ncol=5)
testm[,1] <- c(1,2,3,4,5)
testm[,2] <- c(3,3,3,3,3)
testm[,3] <- c(3,3,4,4,3)
testm[,4] <- c(5,4,3,2,1)
testm[,5] <- c(2,2,3,4,4)
testm
test3 <- testm[,apply(testm,2,function(x) all(c(TRUE,x[-length(x)]!=x[-1])))]
test3
test3
[,1] [,2]
[1,] 1 5
[2,] 2 4
[3,] 3 3
[4,] 4 2
[5,] 5 1
Thanks,
Ben W.
________________________________________
From: arun [[email protected]]
Sent: 26 January 2013 02:34
To: Benjamin Ward (ENV)
Cc: R help
Subject: Re: [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) <[email protected]>
To: "[email protected]" <[email protected]>
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): [email protected]
The Sainsbury Laboratory: [email protected]
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.
______________________________________________
[email protected] 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.