Neal Fultz
Neal Fultz

Reputation: 9687

How do I remove duplicated columns from a data frame in R?

I have a data.frame containing many duplicated columns, for example:

df = data.frame(a=1:10, b=1:10, c=2:11)

Is there a function (base R or dplyr) that removes duplicated columns ? unique() removes duplicate rows.

Unlike How to remove duplicated column names in R? my columns already have different names, but the values are identical.

Upvotes: 2

Views: 2489

Answers (1)

akrun
akrun

Reputation: 887028

An option is

df[!duplicated(as.list(df))]

Or

df[!duplicated(unclass(df))]

Upvotes: 5

Related Questions