Gero
Gero

Reputation: 157

dataframe %in% dataframe. (R programming)

I am trying to extract in a specific way certain rows from a dataset using a logical vector:

Logical.vector <- df %in% df[df$a=='ABC123',]
Logical.vector
-----------------------------------------
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Being df[df$a=='ABC123',] multiple rows (a "subdataframe") extracted from a dataframe (df) with the next dimensions:

ndim(df) 
---------------------------------------
[1] 4226    11

Why is my code not obtaining a successful result? Which way could I obtain a logical vector for the rows of the dataframe? Thank you in advance!

Upvotes: 1

Views: 44

Answers (1)

akrun
akrun

Reputation: 887148

The reason is that %in% works on vector and not on a list or data.frame. For that we need == (but this works only for a single element). If we want to use %in%, then paste each row to a single string

i1 <- do.call(paste, c(df, sep=", ")) %in% 
             do.call(paste,  c(df[df$a=='ABC123',], sep=", "))

Upvotes: 2

Related Questions