0

I have two data frames that share the same factor, but don't repeat the levels the same number of times. I need to import the values from a variable in the second data frame into the first, according to factor level. Here's an example of what the two data frames look like:

First DF:

User   Issue Logged     Other.Variable    Other.Variable    Quartile
A      File Download
A      Log-In
A      File Download
B      File Upload
C      File Upload
C      File Upload
D      Log-In
D      File Download
D      File Upload
D      Log-In

Second DF:

User   Issue.Frequency   Quartile
A      3                 3
B      1                 1
C      2                 2
D      4                 4

I would like to put the Quartile value from the 2nd DF into the 1st DF so that I can subset that 1st DF according to the Quartile Ranks. I tried using an ifelse statement:

DF1$Quartile <- ifelse (DF1$User %in% DF2$User, DF2$Quartile, "Failed")

But this simply repeated the DF2 Quartile values without actually matching them to their associated user:

First DF:

User   Issue Logged     Other.Variable    Other.Variable    Quartile
A      File Download                                        3
A      Log-In                                               1
A      File Download                                        2
B      File Upload                                          4
C      File Upload                                          3
C      File Upload                                          1
D      Log-In                                               2
D      File Download                                        4
D      File Upload                                          3
D      Log-In                                               1

When what I want is:

First DF:

User   Issue.Logged     Other.Variable    Other.Variable    Quartile
A      File Download                                        3
A      Log-In                                               3
A      File Download                                        3
B      File Upload                                          1
C      File Upload                                          2
C      File Upload                                          2
D      Log-In                                               4
D      File Download                                        4
D      File Upload                                          4
D      Log-In                                               4

I understand why my first ifelse statement failed (it was simply imputing the Quartile variable for every match that returned true), but I don't know how to proceed so that the quartile values are placed according to the associated user (factor level). I also tried some other variations of the ifelse statement, but ran into errors such as:

for (i in DF1$User %in% DF2$User) DF1$Quartile <- DF2$Quartile
Error in `$<-.data.frame`(`*tmp*`, "Quartile", value = c(1L, 1L, 1L, 1L,  :   
 replacement has 4 rows, data has 10

Can someone set me straight on this? Or provide some direction to resources that will help me figure this out?

Thanks in advance!

0 Answers0