Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 连接和合并两个无多重性的数据帧_R_Join_Merge - Fatal编程技术网

R 连接和合并两个无多重性的数据帧

R 连接和合并两个无多重性的数据帧,r,join,merge,R,Join,Merge,我知道这个标题很让人困惑,但我必须解释我的问题。 我有两个数据集,其中第一个数据集包含由pmid表示的每篇文章的年引用频率。它看起来像这样: pmid year freq 1 14561399 2011 1 2 14561399 2012 3 3 18511332 2010 1 4 21193046 2012 2 5 21193046 2013 2 6 14561399 2013 1 7 18511332 2011 1 8

我知道这个标题很让人困惑,但我必须解释我的问题。 我有两个数据集,其中第一个数据集包含由pmid表示的每篇文章的年引用频率。它看起来像这样:

       pmid year freq
1  14561399 2011    1
2  14561399 2012    3
3  18511332 2010    1
4  21193046 2012    2
5  21193046 2013    2
6  14561399 2013    1
7  18511332 2011    1
8  18511332 2012    3
9  14561399 2014    1
10 16533158 2013    2
      pmid title_char title_wrds
1 20711763         75          9
2 20734175        109         12
3 20058113         93         13
4 20625865        142         17
5 20517661        103         12
6 20195930        128         16
   pmid title_char title_wrds year freq
1   184         77         10 2010    1
2   406        142         20 2008    1
3   407        110         16 2008    1
4   407        110         16 2003    1
5   408         79         10 1998    1
6   450         58          7 2012    2
7   450         58          7 2009    1
    pmid title_char title_wrds year2008Freq year2003Freq
 1   407        110         16            1            1
第二个包含文章的功能,如下所示:

       pmid year freq
1  14561399 2011    1
2  14561399 2012    3
3  18511332 2010    1
4  21193046 2012    2
5  21193046 2013    2
6  14561399 2013    1
7  18511332 2011    1
8  18511332 2012    3
9  14561399 2014    1
10 16533158 2013    2
      pmid title_char title_wrds
1 20711763         75          9
2 20734175        109         12
3 20058113         93         13
4 20625865        142         17
5 20517661        103         12
6 20195930        128         16
   pmid title_char title_wrds year freq
1   184         77         10 2010    1
2   406        142         20 2008    1
3   407        110         16 2008    1
4   407        110         16 2003    1
5   408         79         10 1998    1
6   450         58          7 2012    2
7   450         58          7 2009    1
    pmid title_char title_wrds year2008Freq year2003Freq
 1   407        110         16            1            1
如您所见,这两个数据集都包含pmid,这是我需要合并或加入此数据集的参数。这不是问题,可以使用justmerge函数或dplyr包来完成。但当我这样做时,结果如下所示:

       pmid year freq
1  14561399 2011    1
2  14561399 2012    3
3  18511332 2010    1
4  21193046 2012    2
5  21193046 2013    2
6  14561399 2013    1
7  18511332 2011    1
8  18511332 2012    3
9  14561399 2014    1
10 16533158 2013    2
      pmid title_char title_wrds
1 20711763         75          9
2 20734175        109         12
3 20058113         93         13
4 20625865        142         17
5 20517661        103         12
6 20195930        128         16
   pmid title_char title_wrds year freq
1   184         77         10 2010    1
2   406        142         20 2008    1
3   407        110         16 2008    1
4   407        110         16 2003    1
5   408         79         10 1998    1
6   450         58          7 2012    2
7   450         58          7 2009    1
    pmid title_char title_wrds year2008Freq year2003Freq
 1   407        110         16            1            1
我的问题是——正如你可以看到的例子第2行和第3行——这两行包含相同的文章,相同的pmid,相同的功能,但由于引用年份的原因,它在两行中

    pmid title_char title_wrds year freq
 3   407        110         16 2008    1
 4   407        110         16 2003    1
我想要这样的东西:

       pmid year freq
1  14561399 2011    1
2  14561399 2012    3
3  18511332 2010    1
4  21193046 2012    2
5  21193046 2013    2
6  14561399 2013    1
7  18511332 2011    1
8  18511332 2012    3
9  14561399 2014    1
10 16533158 2013    2
      pmid title_char title_wrds
1 20711763         75          9
2 20734175        109         12
3 20058113         93         13
4 20625865        142         17
5 20517661        103         12
6 20195930        128         16
   pmid title_char title_wrds year freq
1   184         77         10 2010    1
2   406        142         20 2008    1
3   407        110         16 2008    1
4   407        110         16 2003    1
5   408         79         10 1998    1
6   450         58          7 2012    2
7   450         58          7 2009    1
    pmid title_char title_wrds year2008Freq year2003Freq
 1   407        110         16            1            1
这是每1篇文章1行。

你可以试试

library(reshape2)
res <- dcast(dfN, ...~paste0('year', year, 'Freq'), value.var='freq')
数据
重塑fn,idvar=c'pmid',title\u char',title\u wrds',timevar='year',direction='wide'有帮助吗?或使用图书馆2;dcastdfN,…~year,value.var='freq'问题已解决,谢谢!