[R] to remove columns and rows

David L Carlson dcarlson at tamu.edu
Wed Aug 15 04:40:51 CEST 2012


This should work if your data consist of integer values:

> dput(file1)   # This is the preferred way to send your data
structure(list(V1 = c(1L, 1L, 1L, 1L, 1L, 1L), V2 = c(0L, 0L, 
0L, 0L, 0L, 0L), V3 = c(2L, 2L, 2L, 2L, 2L, 2L), V4 = c(2L, 2L, 
2L, 2L, 2L, 2L), V5 = c(1L, 1L, 1L, 1L, 1L, 1L), V6 = c(1L, 1L, 
2L, 1L, 0L, 1L), V7 = c(5L, 5L, 5L, 5L, 5L, 5L), V8 = c(1L, 1L, 
2L, 1L, 0L, 1L), V9 = c(1L, 1L, 2L, 1L, 2L, 0L), V10 = c(1L, 
1L, 1L, 1L, 1L, 1L)), .Names = c("V1", "V2", "V3", "V4", "V5", 
"V6", "V7", "V8", "V9", "V10"), class = "data.frame", row.names = c(NA, 
-6L))
> file2 <- file1[, sapply(file1, function(x) any(diff(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

There is a typo in your message. Columns 1, 2, 3, 4, 5, 7, and 10 have all
elements duplicated and 6, 8, and 9 do not.
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Fabiane Silva
> Sent: Tuesday, August 14, 2012 8:24 PM
> To: r-help at r-project.org
> 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