如何在R中合并两个不相等大小的数据帧 [英] how to merge two unequal size data frame in R

查看:73
本文介绍了如何在R中合并两个不相等大小的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框:

>temp

      Var1                Freq1
1   file-upload             1
2   image-processing        1
3     mime-types            1
4       php                 5


>top 

        Var2               Freq2
1   file-upload             1
2   image-processing        1
3     mime-types            1
4       php                 5
5      upload               1
6      firefox              2
7   machine-learning        1
8     matlab                1
9        r                  2
10      c#                  7

现在我正在做

m1 <- merge(temp, top, by.x = "Var1", by.y = "Var2", all.x = TRUE)

他们m1将是:

       Var1               Freq1        Freq2
1   file-upload             1            1
2   image-processing        1            1
3     mime-types            1            1
4       php                 5            5

,但是m1$Var1的级别数显示10而不是4,并且当我尝试根据Var1

but the number of levels of m1$Var1 is showing 10 not 4 and when I am trying to do a split of m1 based on the Values of Var1

x <- split(m1, m1$Var1)

length(x)是10而不是4,并且6个元素显示了这种值

The length(x) is 10 not 4 and the 6 elements are showing this kind of values

$c#
[1] Var1 Freq1  Freq2  
<0 rows> (or 0-length row.names)

我想从列表中删除这些元素,或者有什么方法可以使合并时的级别数等于temp df.

I want to remove these elements from the list or is there any way that when i am merging I have the number of level equal to that of temp df.

推荐答案

您可以通过以下方式删除空级别

You can remove empty levels by

m1$Var1 <- factor(m1$Var1)

这篇关于如何在R中合并两个不相等大小的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆