0

Input:

a<- c(1,2,2)
b<- c(1,2,2)
c<- c(1,2,3)
d<- c(0,0,0)
e<- c(0,0,0)

f<- cbind(a,b,c,d,e)

What I want (look at the outcome):

whatIwant<- cbind(a,c,d)
  • I do not want to remove the columns by column names because it's a huge dataframe
  • I am not concerned about the column names Thank you!!
LXLX
  • 1

2 Answers2

0

duplicated gives you duplicates row-wise, here you want to find duplicates column wise. So take transpose of the matrix that you have, find out the duplicates and use it to subset columns.

result <- f[, !duplicated(t(f))]
result

#     a c d
#[1,] 1 1 0
#[2,] 2 2 0
#[3,] 2 3 0
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0
unique(f,MARGIN=2)

     a c d
[1,] 1 1 0
[2,] 2 2 0
[3,] 2 3 0
user2974951
  • 9,535
  • 1
  • 17
  • 24