JKay96
JKay96

Reputation: 73

R - Removing a column in a dataframe, if the column values are the same as another column

I've inserted a table to make it clearer, basically, I'm trying to find a way to get rid of other columns that are identical in the values it has to another one, a duplicate column. enter image description here

As we can see in the image, Col 2 and 4 are identical, I want to remove Col 4, because for my use it's not helping and is unneeded data.

Thanks!

Upvotes: 1

Views: 44

Answers (1)

akrun
akrun

Reputation: 887028

We can use duplicated on the transpose of the dataset to create a logical index and use that to subset the columns

df1[!duplicated(t(df1))]
#    Col1 Col2 Col3 Col5
#1    1    2    3    1
#2    2    3    4    2
#3    3    4    1    4
#4    4    1    2    3

Upvotes: 3

Related Questions