bbslover
2009-Nov-06 07:28 UTC
[R] how can I delete those columes with the same element in every row?
e.g. a a b c d e 1 1 1 3 1 1 2 1 2 3 4 5 3 1 3 3 8 3 4 1 4 3 3 5 5 1 1 3 1 1 I want to delete colume a and colume c, because they have the same values in every row, then ,I want to get this data.frame . b b d e 1 1 1 1 2 2 4 5 3 3 8 3 4 4 3 5 5 1 1 1 the following is my code but it's wrong. rm(list=ls()) a=c(1,1,1,1,1); b=c(1,2,3,4,1); c=c(3,3,3,3,3); d=c(1,4,8,3,1); e=c(1,5,3,5,1) data.f=data.frame(a,b,c,d,e) origin.data<-data.f dim.frame=dim(data.f) rn=dim.frame[1] n<-0 for (k in 1:(dim.frame[2]-n)) {if (data.f[1,k]==data.f[rn,k]) {data.f<-data.f[,-k] n<-n+1 k<-k-1 } } origin.data data.f how can i modify it and obtain my wanted result. thank you! -- View this message in context: http://old.nabble.com/how-can-I-delete-those-columes-with-the-same-element-in-every-row--tp26227873p26227873.html Sent from the R help mailing list archive at Nabble.com.
Berend Hasselman
2009-Nov-06 09:34 UTC
[R] how can I delete those columes with the same element in every row?
bbslover wrote:> > e.g. > > a> a b c d e > 1 1 1 3 1 1 > 2 1 2 3 4 5 > 3 1 3 3 8 3 > 4 1 4 3 3 5 > 5 1 1 3 1 1 I want to delete colume a and colume c, because they > have the same values in every row, then ,I want to get this data.frame . > > b> b d e > 1 1 1 1 > 2 2 4 5 > 3 3 8 3 > 4 4 3 5 > 5 1 1 1 >Try this xdf <- apply(data.f, 2, function(x) x==x[1]) data.f <- data.f[,!apply(xdf,2,all)] data.f -- View this message in context: http://old.nabble.com/how-can-I-delete-those-columes-with-the-same-element-in-every-row--tp26227873p26228799.html Sent from the R help mailing list archive at Nabble.com.
bbslover
2009-Nov-06 10:45 UTC
[R] how can I delete those columes with the same element in every row?
it is perfect?you give me a great help. thanks R and Berend Hasselman, my friends. but I think I do have more questions to ask. thank you! Berend Hasselman wrote:> > > > bbslover wrote: >> >> e.g. >> >> a>> a b c d e >> 1 1 1 3 1 1 >> 2 1 2 3 4 5 >> 3 1 3 3 8 3 >> 4 1 4 3 3 5 >> 5 1 1 3 1 1 I want to delete colume a and colume c, because they >> have the same values in every row, then ,I want to get this data.frame . >> >> b>> b d e >> 1 1 1 1 >> 2 2 4 5 >> 3 3 8 3 >> 4 4 3 5 >> 5 1 1 1 >> > > Try this > > xdf <- apply(data.f, 2, function(x) x==x[1]) > data.f <- data.f[,!apply(xdf,2,all)] > data.f > > >-- View this message in context: http://old.nabble.com/how-can-I-delete-those-columes-with-the-same-element-in-every-row--tp26227873p26229883.html Sent from the R help mailing list archive at Nabble.com.