R将一个长格式数据帧与另一个数据帧匹配

时间:2016-11-14 06:05:57

标签: r dataframe merge dplyr

我有两个数据框,一个用于匹配,另一个是数据。匹配的是长格式。

  #long format df:

    Person  variable    type    value
1   claire  1   T   0.7396
2   murry   1   C   1.517824
3   kidson  1   T   1.686622
4   kai 1   T   1.4884
5   migu    1   A   1.2769
6   poka    1   G   1.5129
7   nomi    1   C   0.49
8   claire  2   TC  0.86
9   murry   2   CT  1.232
10  kidson  2   CT  1.2987
11  kai 2   CT  1.22
12  migu    2   GA  1.13
13  poka    2   GT  1.23
14  nomi    2   TC  0.7
15  claire  3   C   1
16  murry   3   T   1
17  kidson  3   C   1
18  kai 3   C   1
19  migu    3   G   1
20  poka    3   T   1
21  nomi    3   T   1

lfdf = structure(list(Person = c("claire", "murry", "kidson", "kai", 
"migu", "poka", "nomi", "claire", "murry", "kidson", "kai", "migu", 
"poka", "nomi", "claire", "murry", "kidson", "kai", "migu", "poka", 
"nomi"), variable = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), type = c("T", "C", 
"T", "T", "A", "G", "C", "TC", "CT", "CT", "CT", "GA", "GT", 
"TC", "C", "T", "C", "C", "G", "T", "T"), value = c(0.7396, 1.517824, 
1.686622, 1.4884, 1.2769, 1.5129, 0.49, 0.86, 1.232, 1.2987, 
1.22, 1.13, 1.23, 0.7, 1, 1, 1, 1, 1, 1, 1)), .Names = c("Person", 
"variable", "type", "value"), class = "data.frame", row.names = c(NA, 
21L))

#df:
Person  SB001   SB002   SB003   SB004
1   claire  C   T   T   TC
2   murry   T   T   T   T
3   kidson  CT  CT  T   T
4   kai C   T   CT  CT
5   migu    G   G   G   GA
6   poka    GT  G   T   T
7   nomi    TC  TC  T   TC

df <- structure(list(Person = c("claire", "murry", "kidson", "kai", 
"migu", "poka", "nomi"), SB001 = c("C", "T", "CT", "C", "G", 
"GT", "TC"), SB002 = c("T", "T", "CT", "T", "G", "G", "TC"), 
SB003 = c("T", "T", "T", "CT", "G", "T", "T"), SB004 = c("TC", 
"T", "T", "CT", "GA", "T", "TC")), .Names = c("Person", "SB001", 
"SB002", "SB003", "SB004"), class = "data.frame", row.names = c(NA, 
7L))

desired output:
    Person  SB001   SB002   SB003   SB004
1   claire  1   0.7396  0.7396  0.86
2   murry   1   1   1   1
3   kidson  1.2987  1.2987  1.686622    1.686622
4   kai 1   1.4884  1.22    1.22
5   migu    1   1   1   1.13
6   poka    1.23    1.5129  1   1
7   nomi    0.7 0.7 1   0.7

我想在另一个数据帧中将类型与值匹配,但在我将其转换为长格式数据帧后,我不知道如何将其与所需的输出相匹配。

感谢您的帮助 有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

如果您将df转换为长格式,则更易于管理。然后你就可以执行查找了。

您可以使用tidyverse包:

df %>%
  gather(Id, value, -Person) %>%
  left_join(lfdf, by= c("Person" = "Person", "value" = "type")) %>%
  select(Person, Id, value.y) %>%
  spread(Id, value.y)

答案 1 :(得分:1)

我们可以使用E/AndroidRuntime: FATAL EXCEPTION: main java.lang.StackOverflowError at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:0) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20) at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.jav 中的meltdcast来执行此操作,如下所示,

注意我将名称reshape2value更改为lfdf,因为它与value1给出的value名称重叠

melt