Al14
Al14

Reputation: 1814

subsetting between two data frames

I want to subset everything from df1 except df2.

df1<-    
A  B  C  D  E  F  G  H  I  J
80 16 55 74 89 39  4 67 36 87
69 49 91 83 50  1 77 19 73 43
85 45 97  9 47 65 79 81 86 66
37 58 17 38 76 14 54 78 62 98
12 25 56 20 31 82 34 23 33 11

df2<-
C  D  E  F
55 74 89 39
91 83 50  1
97  9 47 65
17 38 76 14 
56 20 31 82 

I would like to utilise this kind of approach if possible:

mydata<-df1[,!colnames(df2)]

Upvotes: 0

Views: 50

Answers (1)

Cliff AB
Cliff AB

Reputation: 1190

If you want the columns that are in df1, but not in df2, this can be done as such:


    not_in_df2 <- setdiff(colnames(df1), colnames(df2))
    subSet_df1 <- df1[,not_in_df2]
    

Or you could define not_in_df2 via


    not_in_df2 <- !(colnames(df1) %in% colnames(df2))
    

Upvotes: 2

Related Questions