[R] to remove columns and rows

arun smartpink111 at yahoo.com
Wed Aug 15 05:05:15 CEST 2012


Hello,

Try this:
 idlength<-sapply(file1,function(x) length(unique(x)) )
 which(idlength>1)
#V6 V8 V9 
 #6  8  9 
#the ones that will be removed

which(idlength==1)
# V1  V2  V3  V4  V5  V7 V10 
  #1   2   3   4   5   7  10 
idlength2<-apply(file2,1,function(x) length(unique(x)))
 which(idlength2>1)
#[1] 5 6

#the ones that will be removed

 which(idlength2==1)
[1] 1 2 3 4


In addition to the method i posted earlier, you could also use:

file2<-file1[apply(file1,2, function(x) any(is.na(match(x,x[duplicated(x)]))))]
file2
 #V6 V8 V9
#1  1  1  1
#2  1  1  1
#3  2  2  2
#4  1  1  1
#5  0  0  2
#6  1  1  0


file3<-file2[t(apply(t(file2),2, function(x) any(is.na(match(x,x[duplicated(x)]))))),]
  file3
#  V6 V8 V9
#5  0  0  2
#6  1  1  0

A.K.




________________________________
From: Fabiane Silva <fabianezte at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Tuesday, August 14, 2012 10:46 PM
Subject: Re: [R] to remove columns and rows


Thanks a lot . The program below remove columns and row, but I would like know which ID of columns and rows that 
were remove.



2012/8/14 arun <smartpink111 at yahoo.com>

Hello,
>Try this:
>
>file1<-read.table(text="
>
>1  0  2  2  1  1  5  1  1  1
>1  0  2  2  1  1  5  1  1  1
>1  0  2  2  1  2  5  2  2  1
>1  0  2  2  1 1  5  1  1  1
>1  0 2  2  1  0  5  0  2  1
>1  0  2 2  1  1  5  1  0  1
>",sep="",header=FALSE)
> idlength<-sapply(file1,function(x) length(unique(x)) )
> file2<-subset(file1,select=idlength>1)
> file2
>#  V6 V8 V9
>#1  1  1  1
>#2  1  1  1
>#3  2  2  2
>#4  1  1  1
>#5  0  0  2
>#6  1  1  0
>
>idlength2<-apply(file2,1,function(x) length(unique(x)))
>
> file3<-subset(t(file2),select=idlength2>1)
>file3<-t(file3)
> file3
>#  V6 V8 V9
>#5  0  0  2
>#6  1  1  0
>A.K.
>
>
>
>
>----- Original Message -----
>From: Fabiane Silva <fabianezte at gmail.com>
>To: r-help at r-project.org
>Cc:
>Sent: Tuesday, August 14, 2012 9:24 PM
>Subject: [R] to remove columns and rows
>
>Dear,
>
>
>
>I am using R I'm trying to identify and remove columns and rows in a data
>frame that are has elements equals. For example in dataframe below. The
>columns 1, 2,3,4,5 ,6 and 10 (file1) has elements equal then should be
>removed. How can I ask R to remove those columns with same elements in new
>dataframe (file2) to result a matrix as follows:
>
>
>
>file1
>
>1  0  2  2  1  1  5  1  1  1
>
>1  0  2  2  1  1  5  1  1  1
>
>1  0  2  2  1  2  5  2  2  1
>
>1  0  2  2  1 1  5  1   1  1
>
>1  0 2  2  1  0  5  0  2  1
>
>1  0  2 2  1  1  5  1  0  1
>
>
>
>file2
>
>1   1  1
>
>1   1  1
>
>2   2  2
>
>1   1  1
>
>0   0  2
>
>1   1  0
>
>
>
>After I need to know which ones names of columns are remove = 1, 2,3,4,5 ,6
>and 10.
>
>
>
>Similarly this is I need to check for rows in file2. To remove rows and
>identify all that elements equal.
>
>The final dataframe is:
>
>
>
>file3
>
>
>
>0   0  2
>
>1   1  0
>
>
>
>row names removed = 1,2,3 and
>
>
>
>Thanks and Regards
>
>    [[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.
>
>



More information about the R-help mailing list