Muhammad Kamil
Muhammad Kamil

Reputation: 665

Getting common columns names in R from two data frames

I have two vectors containing strings:

names_1 <- c("A", "B", "C", "D")
names_2 <- c("C", "D", "E")

How do I know which strings from names_1 exist in names_2?

Upvotes: 0

Views: 55

Answers (2)

ccommjin
ccommjin

Reputation: 87

which(names_1 %in% names_2)

This will return the index of selected strings.

Upvotes: 1

huan
huan

Reputation: 308

If you want to to get a logical vector, you could use:

is.element(names_1, names_2)

If you want to get the elements, which are the same, you could use:

intersect(names_1, names_2)

Upvotes: 1

Related Questions