F Roijers
F Roijers

Reputation: 11

How to delete rows based on condition in embedded data frame?

I am working with the MarketMatching package in R.

MarketMatching gives a list of 5 comparison Markets for my target Market. However, I want to remove specific markets from this list.

The MarketMatching object looks like this in R studio: Screenshot

  1. Level 1: mm is a list[5]
  2. Level 2: BestMatches is a list[5x8](S3: data.frame)
  3. Level 3: BestControl is a character[5]

I tried it like this (where 'mm' is the object described above), 'BestControl' is a column with values "PL", "FI", "NG", "GH", "RU". So I want to delete GH and NG from that list.

active_campaigns <- c("GH", "NG")
mm$BestMatches <- mm$BestMatches[!(BestControl %in% active_campaigns),]

Out:

Error in BestControl %in% active_campaigns : object 'BestControl' not found

In other words: How can I access a column of a dataframe that is embedded in a list to drop rows there?

Upvotes: 1

Views: 450

Answers (1)

Yacine Hajji
Yacine Hajji

Reputation: 1449

You can subset the rows you want (i.e. no character from BestControl within active_campaigns).

# 1- Create a function for testing boolean "not within"
'%!in%' <- function(x,y)!('%in%'(x,y))

# 2- Subset the string not within active_campaigns
mmSubset <- subset(mm$BestMatches, BestControl %!in% active_campaigns)

Upvotes: 1

Related Questions