Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find rows in a data frame where two columns are equal

Tags:

r

I'd like to select the rows in a data frame where two columns, A and B are equal. I have tried this:

A <- c(0,1,2)
B <- c(2,1.000001,0)
df <- as.data.frame(cbind(A,B))
subset(df,A==B) # does not work
# [1] A B
# <0 rows> (or 0-length row.names)
subset(df,round(A,3)==round(B,3)) # does work
#  A        B
# 2 1 1.000001
subset(df,A==B)
like image 955
LeelaSella Avatar asked Dec 15 '11 12:12

LeelaSella


1 Answers

mteq <- mtcars[mtcars$gear==mtcars$carb, ]
like image 121
nzcoops Avatar answered Oct 01 '22 19:10

nzcoops