如何有条件地为每个与R中的不同因子匹配的因子级别分配值?

时间:2016-09-07 19:58:01

标签: r if-statement matching factors

我有两个共享相同因素的数据框,但不要重复相同次数的级别。我需要根据因子级别将第二个数据框中的变量值导入第一个数据框。以下是两个数据框的示例:

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

第二次DF:

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

我想将第二个DF中的Quartile值放入第一个DF,这样我就可以根据四分位数对第一个DF进行子集化。我尝试使用ifelse语句:

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

但这只是重复了DF2四分位数值,而没有将它们与相关用户实际匹配:

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

当我想要的是:

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

我理解为什么我的第一个ifelse语句失败了(它只是为每个返回true的匹配输入Quartile变量),但我不知道如何继续,以便根据相关用户放置四分位数值(因子水平)。我还尝试了ifelse语句的其他一些变体,但遇到了错误,例如:

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

有人能让我直截了当吗?或者为资源提供一些指导,帮助我解决这个问题?

提前致谢!

0 个答案:

没有答案