0

Let me explain my problem a little more clearly. Let's say I have two dataframes, A and B, and they look something like this.

Dataframe A 
   Names    Email
1   John    [email protected]
2   Harry   [email protected] 
3   Amy     [email protected]
4   Jim     [email protected]
5   Chad    [email protected]

Dataframe B 
   Email    Category
1  [email protected]  Student
2  [email protected]  Faculty
3  [email protected]  Alumni
4  [email protected]  Student
5  [email protected]  Alumni

My goal is to create a new column in dataframe A called Category, which will have a value equal to B$Category if a value in A$Email matches any value in B$Email, and NA otherwise. Here is my ideal output

Dataframe A
   Names    Email     Category
1   John    [email protected]   Alumni
2   Harry   [email protected]   NA
3   Amy     [email protected]   Student
4   Jim     [email protected]   NA
5   Chad    [email protected]   NA

What would be the best way to go about this?

thpoole
  • 15
  • 2

1 Answers1

0

This is a join or merge operation. In this case, a left-join:

merge(dfA, dfB, by.x = "Email", all.x = TRUE)
#     Email Names Category
# 1 [email protected]  John   Alumni
# 2 [email protected] Harry     <NA>
# 3 [email protected]   Amy  Student
# 4 [email protected]   Jim     <NA>
# 5 [email protected]  Chad     <NA>

Two good links for learning about it and how to use it in general and in R:


Data:

dfA <- structure(list(Names = c("John", "Harry", "Amy", "Jim", "Chad"), Email = c("[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]")), class = "data.frame", row.names = c("1", "2", "3", "4", "5"))
dfB <- structure(list(Email = c("[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"), Category = c("Student", "Faculty", "Alumni", "Student", "Alumni")), class = "data.frame", row.names = c("1", "2", "3", "4", "5"))
r2evans
  • 141,215
  • 6
  • 77
  • 149