Dear Sir,

Thanks a lot for your wonderful solution. When I applied it my data.frame, however, it was deleting many other columns also having repeated type of column names i.e. suppose I wanted only to delete say ABC.1, ABC.2 etc. and retain XYZ, XYZ.1, XYZ2 etc. But this was not happening and alongwith ABC series, it was deleting XYZ series too. So I changed the command you had given as -

df[ -grep( "\\.", names( df))] to

df[ -grep( "XYZ\\.", names( df))]

And it lead me to the desired result.

Thanks again sir.

Regards

Katherine




--- On Thu, 28/3/13, Gerrit Eichner <Gerrit.Eichner@math.uni-giessen.de> wrote:

From: Gerrit Eichner <Gerrit.Eichner@math.uni-giessen.de>
Subject: Re: [R] How to delete Identical columns
To: "Katherine Gobin" <katherine_gobin@yahoo.com>
Cc: r-help@r-project.org
Date: Thursday, 28 March, 2013, 8:58 AM

Hi, Katherine,

IF the naming scheme of the columns of your data frame is consistently 
<stringwithoutdot> and <stringwithoutdot.number> if duplicated columns 
appear THEN (something like)

df[ -grep( "\\.", names( df))]

could help. (But it's maybe more efficient to avoid - a priori - producing 
duplicated columns, if the data frame is large, as you say.)

  Regards -- Gerrit


On Thu, 28 Mar 2013, Katherine Gobin wrote:

> Dear R forum
>
> Suppose I have a data.frame
>
> df = data.frame(id = c(1:6), x = c(15, 21, 14, 21, 14, 38), y = c(36, 38, 55, 11, 5, 18), x.1 = c(15, 21, 14, 21, 14, 38), z = c("D", "B", "A", "F", "H", "P"))
>
>
>> df
>   id  x  y    x.1 z
> 1  1 15 36  15 D
> 2  2 21 38  21 B
> 3  3 14 55  14 A
> 4  4 21 11  21 F
> 5  5 14  5  14 H
> 6  6 38 18  38 P
>
>
> Clearly columns x and x.1 are identical. In reality, I have a large data.frame and can't make out which columns are identical, but I am sure that column with name say x is repeated as x.1, x.2 etc.
>
> How to automatically identify and retain only one column (in this example column x) among the identical columns besides other non-identical columns (viz. id, y and z).
>
>
> Regards
>
> Katherine
	[[alternative HTML version deleted]]

