Joe
Joe

Reputation: 193

Comparing two columns of different length in R

I am trying to implement vlookup type functionality in R. I have two data frames - Id column has unique values.

# DataFrame1 - df1
Data   ID       C1        C2      C3 
R1     ax       12        33      11
R2     ay       11         2       0
R3     az        3         3       0
R4     ar        2         3       1
R5     ag        2         3       1

# DataFrame2 - df2
Id
ax
ay

I want to get all the data and get its count from DataFrame1 for which there is a match between Ids from DataFrame2 and DataFrame1.

Output should be -

Data ID C1 C2 C3
R1   ax 12 33 11
R2   ay 11  2  0

and its count will be 2

Also, is this possible without merge function?

Upvotes: 1

Views: 3494

Answers (2)

desc
desc

Reputation: 1210

To subset your first dataframe by your second, then count the number of rows:

z=df1[df1$ID %in% df2$Id,]
count=nrow(z)

Upvotes: 3

mpalanco
mpalanco

Reputation: 13570

Using dplyr:

library(dplyr)
inner_join(df1, df2, by = c("ID" = "Id")) # Because Id columns names are different
inner_join(df1, df2) # If the Id columns were the same

Using sqldf:

library(sqldf)
sqldf('SELECT * FROM df1 JOIN df2 USING (ID)') # Not case sensitive

Output:

  Data ID C1 C2 C3
1   R1 ax 12 33 11
2   R2 ay 11  2  0

Upvotes: 0

Related Questions