Skip to content

How to select a whole column? Thanks!

4 messages · Jinsong Zhao, Brian Ripley, Paul Lemmens

#
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
#
On Tue, 3 Aug 2004, Jinsong Zhao wrote:

            
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]
which suggests you read `An Introduction to R', and that covers this and 
more.
#
Hoi Jinsong,

--On dinsdag 3 augustus 2004 1:42 -0700 Jinsong Zhao <jinsong_zh at yahoo.com> 
wrote:
subset(df, select=-c(V3,V4,V5,V6))
1 day later
#
Thank all of you for your kindly help.
--- Jinsong Zhao <jinsong_zh at yahoo.com> wrote:

            
=====
(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