Dear all, I hope to remove a whole column from a data frame or matrix (> 2000 columns). All value in the column are same. The first thing is to select those columns. For instance, I hope to remove the V3~6 column, for all the value in those colume is zero. V3 V4 V5 V6 V7 V8 V9 V10 1 0 0 0 0 0.000 0.000 0.000 0.000 2 0 0 0 0 0.000 0.000 0.000 0.000 3 0 0 0 0 0.000 0.000 0.000 0.000 4 0 0 0 0 0.000 0.000 0.000 0.000 5 0 0 0 0 0.000 0.000 0.000 0.000 6 0 0 0 0 -0.001 -0.001 -0.001 -0.001 7 0 0 0 0 0.000 0.000 0.000 -0.001 8 0 0 0 0 0.000 0.000 0.000 -0.001 9 0 0 0 0 -0.009 -0.012 -0.015 -0.018 I mean how to select the first four columns. Thank you very much for your consideration on this matter. Best wishes, Jinsong ====(Mr.) Jinsong Zhao Ph.D. Candidate School of the Environment Nanjing University 22 Hankou Road, Nanjing 210093 P.R. China E-mail: jinsong_zh at yahoo.com
What about:
my.df<-data.frame(x=rnorm(20),y=rep(0,20),z=rep(1,20))
my.df[,!apply(my.df,2,sd)==0,drop=F]
This uses the fact that a column with eaqual values has a standard deviation
of 0.
Regards
Wayne
-----Original Message-----
From: Jinsong Zhao [mailto:jinsong_zh@yahoo.com]
Sent: 03 August 2004 09:43
To: R-Help
Subject: [R] How to select a whole column? Thanks!
Dear all,
I hope to remove a whole column from a data frame or matrix (> 2000
columns). All value in the column are same. The first thing is to
select those columns.
For instance, I hope to remove the V3~6 column, for all the value in
those colume is zero.
V3 V4 V5 V6 V7 V8 V9 V10
1 0 0 0 0 0.000 0.000 0.000 0.000
2 0 0 0 0 0.000 0.000 0.000 0.000
3 0 0 0 0 0.000 0.000 0.000 0.000
4 0 0 0 0 0.000 0.000 0.000 0.000
5 0 0 0 0 0.000 0.000 0.000 0.000
6 0 0 0 0 -0.001 -0.001 -0.001 -0.001
7 0 0 0 0 0.000 0.000 0.000 -0.001
8 0 0 0 0 0.000 0.000 0.000 -0.001
9 0 0 0 0 -0.009 -0.012 -0.015 -0.018
I mean how to select the first four columns.
Thank you very much for your consideration on this matter.
Best wishes,
Jinsong
====(Mr.) Jinsong Zhao
Ph.D. Candidate
School of the Environment
Nanjing University
22 Hankou Road, Nanjing 210093
P.R. China
E-mail: jinsong_zh@yahoo.com
______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
KSS Ltd
Seventh Floor St James's Buildings 79 Oxford Street Manchester M1 6SS
England
Company Registration Number 2800886
Tel: +44 (0) 161 228 0040 Fax: +44 (0) 161 236 6305
mailto:kssg@kssg.com http://www.kssg.com
The information in this Internet email is confidential and m...{{dropped}}
On Tue, 3 Aug 2004, Jinsong Zhao wrote:> I hope to remove a whole column from a data frame or matrix (> 2000 > columns). All value in the column are same. The first thing is to > select those columns. > > For instance, I hope to remove the V3~6 column, for all the value in > those colume is zero. > > V3 V4 V5 V6 V7 V8 V9 V10 > 1 0 0 0 0 0.000 0.000 0.000 0.000 > 2 0 0 0 0 0.000 0.000 0.000 0.000 > 3 0 0 0 0 0.000 0.000 0.000 0.000 > 4 0 0 0 0 0.000 0.000 0.000 0.000 > 5 0 0 0 0 0.000 0.000 0.000 0.000 > 6 0 0 0 0 -0.001 -0.001 -0.001 -0.001 > 7 0 0 0 0 0.000 0.000 0.000 -0.001 > 8 0 0 0 0 0.000 0.000 0.000 -0.001 > 9 0 0 0 0 -0.009 -0.012 -0.015 -0.018 > > I mean how to select the first four columns.mydf2 <- mydf[-(1:4)] If you wanted to remove all columns which were entirely zero, you could use cols <- sapply(mydf, function(x) all(x == 0)) mydf2 <- mydf[!cols]> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlwhich suggests you read `An Introduction to R', and that covers this and more. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hoi Jinsong, --On dinsdag 3 augustus 2004 1:42 -0700 Jinsong Zhao <jinsong_zh at yahoo.com> wrote:> For instance, I hope to remove the V3~6 column, for all the value in > those colume is zero. > > V3 V4 V5 V6 V7 V8 V9 V10 > 1 0 0 0 0 0.000 0.000 0.000 0.000 > 2 0 0 0 0 0.000 0.000 0.000 0.000 > 3 0 0 0 0 0.000 0.000 0.000 0.000 > 4 0 0 0 0 0.000 0.000 0.000 0.000 > 5 0 0 0 0 0.000 0.000 0.000 0.000 > 6 0 0 0 0 -0.001 -0.001 -0.001 -0.001 > 7 0 0 0 0 0.000 0.000 0.000 -0.001 > 8 0 0 0 0 0.000 0.000 0.000 -0.001 > 9 0 0 0 0 -0.009 -0.012 -0.015 -0.018 > > I mean how to select the first four columns. >subset(df, select=-c(V3,V4,V5,V6)) -- Paul Lemmens NICI, University of Nijmegen ASCII Ribbon Campaign /"\ Montessorilaan 3 (B.01.05) Against HTML Mail \ / NL-6525 HR Nijmegen X The Netherlands / \ Phonenumber +31-24-3612648 Fax +31-24-3616066
Thank all of you for your kindly help. --- Jinsong Zhao <jinsong_zh at yahoo.com> wrote:> Dear all, > > I hope to remove a whole column from a data frame or matrix (> 2000 > columns). All value in the column are same. The first thing is to > select those columns. > > For instance, I hope to remove the V3~6 column, for all the value in > those colume is zero. > > V3 V4 V5 V6 V7 V8 V9 V10 > 1 0 0 0 0 0.000 0.000 0.000 0.000 > 2 0 0 0 0 0.000 0.000 0.000 0.000 > 3 0 0 0 0 0.000 0.000 0.000 0.000 > 4 0 0 0 0 0.000 0.000 0.000 0.000 > 5 0 0 0 0 0.000 0.000 0.000 0.000 > 6 0 0 0 0 -0.001 -0.001 -0.001 -0.001 > 7 0 0 0 0 0.000 0.000 0.000 -0.001 > 8 0 0 0 0 0.000 0.000 0.000 -0.001 > 9 0 0 0 0 -0.009 -0.012 -0.015 -0.018 > > I mean how to select the first four columns. > > Thank you very much for your consideration on this matter. > > Best wishes, > > Jinsong >====(Mr.) Jinsong Zhao Ph.D. Candidate School of the Environment Nanjing University 22 Hankou Road, Nanjing 210093 P.R. China E-mail: jinsong_zh at yahoo.com