Home > OS >  How to delete rows in a Dataframe that have same string entries two or more columns Using R
How to delete rows in a Dataframe that have same string entries two or more columns Using R

Time:03-30

I have this data set and I want to remove the row that possesses the same entries in column 1 and column 2 in the following data frame

         Terms    Terms.1 Freq
1    education  education    3
2    enrolment  education    2
3    household  education    1
4       policy  education    2
5      primary  education    3
6       school  education    3
7   curriculum  education    1
8     research  education    2
9      teacher  education    2
10    teaching  education    2
11   education  enrolment    2
12   enrolment  enrolment    2
13   household  enrolment    1
14      policy  enrolment    1
15     primary  enrolment    2
16      school  enrolment    2
17  curriculum  enrolment    0
18    research  enrolment    1
19     teacher  enrolment    1
20    teaching  enrolment    1
21   education  household    1
22   enrolment  household    1
23   household  household    1
24      policy  household    1
25     primary  household    1
26      school  household    1
27  curriculum  household    0
28    research  household    0
29     teacher  household    0
30    teaching  household    0
31   education     policy    2
32   enrolment     policy    1
33   household     policy    1
34      policy     policy    2
35     primary     policy    2
36      school     policy    2
37  curriculum     policy    1
38    research     policy    1
39     teacher     policy    1
40    teaching     policy    1

The expected data frame should skip the 1st, 12th, 23rd, 34th, 45th Any idea here

CodePudding user response:

df %>% 
  dplyr::filter(Terms != Terms.1)

        Terms   Terms.1 Freq
2   enrolment education    2
3   household education    1
4      policy education    2
5     primary education    3
6      school education    3
7  curriculum education    1
8    research education    2
9     teacher education    2
10   teaching education    2
11  education enrolment    2
13  household enrolment    1
14     policy enrolment    1
15    primary enrolment    2
16     school enrolment    2
17 curriculum enrolment    0
18   research enrolment    1
19    teacher enrolment    1
20   teaching enrolment    1
21  education household    1
22  enrolment household    1
24     policy household    1
25    primary household    1
26     school household    1
27 curriculum household    0
28   research household    0
29    teacher household    0
30   teaching household    0
31  education    policy    2
32  enrolment    policy    1
33  household    policy    1
35    primary    policy    2
36     school    policy    2
37 curriculum    policy    1
38   research    policy    1
39    teacher    policy    1
40   teaching    policy    1

Or with just base R:

df[df$Terms != df$Terms.1,]
  • Related