简体   繁体   中英

How do i get subset of a data frame where Column A value equals Column B value in R ( Data read from CSV)

I am trying to create a subset of a data frame :

Original Data frame looks like :

Column A     Column B    Column C 
---------------------------------
  22        22             30
  18        35             28
  25        25             29
  25        42             22
  75        75             33

I would like to get subset where Column-A value == Column-B Value , End result would look like :

Column A     Column B    Column C 
---------------------------------
      22        22             30
      25        25             29
      75        75             33

Is there any 1 liner solution to achieve this ? Thanks!

Note : I read data from CSV (I haven't provided this data point in original post , sorry).

I get an error when i try : df[df$Column.A==df$Column.B,]

Error in Ops.factor(df$ColumnA, df$ColumnB) : level sets of factors are different

Here's a one-liner:

df1[df1$Column.A==df1$Column.B,]
#  Column.A Column.B Column.C
#1       22       22       30
#3       25       25       29
#5       75       75       33

data

df1 <- read.table(text="Column.A     Column.B    Column.C 
                             22        22             30
                             18        35             28
                             25        25             29
                             25        42             22
                             75        75             33", header=T)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM