Gansaikhan Shur
Gansaikhan Shur

Reputation: 63

R. How to check if a dataset contains the same elements in another dataset

I have 2 datasets Dataset2016_17 and PlayOffDataset2016_17. Dataset2016_17$TEAM looks like the following..

[1] "Atlanta Hawks" "Boston Celtics" "Brooklyn Nets" etc.

So I would like to know if values in Dataset2016_17$TEAM occurs in PlayOffDataset2016_17$TEAM. If so I want something like a table of true and false.

I have already tried something like this

highlight_flag <- grepl(PlayOffDataset2016_17$TEAM, Dataset2016_17$TEAM)

But it did not work. Please let me know if there are any suggestions.

Upvotes: 0

Views: 742

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 145775

In general, you should provide sample inputs and desired output for your question. That helps us understand what you want and get you there quickly.

# sample data 
Dataset2016_17 = data.frame(TEAM = c('a', 'b', 'c'))
PlayOffDataset2016_17 = data.frame(TEAM = c('b', 'c', 'd'))

Your goal isn't super clear, you say "table", but the other answer gave you a table object and that wasn't what you wanted... Next time, in your question, include desired output for the sample input you give.

My best guess is that you want one of these. If you want something else, please edit your question to show what you want.

## teams in both
intersect(Dataset2016_17$TEAM, PlayOffDataset2016_17$TEAM)
# [1] "b" "c"

## teams in Dataset but not playoffs
setdiff(Dataset2016_17$TEAM, PlayOffDataset2016_17$TEAM)
# [1] "a"

## add a column to Dataset indicating whether the team is in the playoffs
Dataset2016_17$in_playoff = Dataset2016_17$TEAM %in% PlayOffDataset2016_17$TEAM
Dataset2016_17
#   TEAM in_playoff
# 1    a      FALSE
# 2    b       TRUE
# 3    c       TRUE

Upvotes: 1

nghauran
nghauran

Reputation: 6768

Try out table(unique(Dataset2016_17$TEAM) %in% unique(PlayOffDataset2016_17$TEAM))

Upvotes: 1

Related Questions