dplyr left_join使用现有列名

时间:2017-05-09 22:13:20

标签: r dplyr

我正在尝试使用dplyr left_join加入data.frames。 更详细地说,我有一个包含多行的data.frame

table <- structure(list(species = c("Adelastes hylonomos", "Adelophryne adiastola", 
                                    "Adelophryne gutturosa", "Adelphobates quinquevittatus", "Adenomera andreae"
), Autor = c("Zweifel, 1986", "Hoogmoed and Lescure, 1984", "Hoogmoed and Lescure, 1984", 
             "(Steindachner, 1864)", "(Mí_ller, 1923)")), .Names = c("species", 
                                                                     "Autor"), row.names = c(NA, 5L), class = "data.frame")
> head(table)
                       species                      Autor
1          Adelastes hylonomos              Zweifel, 1986
2        Adelophryne adiastola Hoogmoed and Lescure, 1984
3        Adelophryne gutturosa Hoogmoed and Lescure, 1984
4 Adelphobates quinquevittatus       (Steindachner, 1864)
5            Adenomera andreae            (Mí_ller, 1923)

我想附加其他数据帧,这些数据帧在一个具体位置只有一行,如物种名称所示。这些可能是其中两个data.frames:

vect1 <- structure(list(species = "Adelophryne adiastola", rango.iucn = "Si", 
                        rango.legend = 2L, rango.area = 17.95164829), .Names = c("species", 
                                                                                 "rango.iucn", "rango.legend", "rango.area"), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                  -1L))
vect2 <- structure(list(species = "Adelophryne gutturosa", rango.iucn = "Si", 
                        rango.legend = 2L, rango.area = 20.94913312), .Names = c("species", 
                                                                                 "rango.iucn", "rango.legend", "rango.area"), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                  -1L))
> head(vect1)
                species rango.iucn rango.legend rango.area
1 Adelophryne adiastola         Si            2   17.95165
> head(vect2)
                species rango.iucn rango.legend rango.area
1 Adelophryne gutturosa         Si            2   20.94913

我希望该功能只能创建一次列,然后再回收它们以添加其他物种数据。但事实并非如此:它附加一个符号(.x / .y):

> table <- table %>% left_join(vect1, by="species")
> table <- table %>% left_join(vect2, by="species")
> head(table)
                       species                      Autor rango.iucn.x
1          Adelastes hylonomos              Zweifel, 1986         <NA>
2        Adelophryne adiastola Hoogmoed and Lescure, 1984           Si
3        Adelophryne gutturosa Hoogmoed and Lescure, 1984         <NA>
4 Adelphobates quinquevittatus       (Steindachner, 1864)         <NA>
5            Adenomera andreae            (Mí_ller, 1923)         <NA>
  rango.legend.x rango.area.x rango.iucn.y rango.legend.y rango.area.y
1             NA           NA         <NA>             NA           NA
2              2     17.95165         <NA>             NA           NA
3             NA           NA           Si              2     20.94913
4             NA           NA         <NA>             NA           NA
5             NA           NA         <NA>             NA           NA

我使用了错误的功能还是我错过了什么? 我真的很想使用dplyr函数。 谢谢你的帮助!

0 个答案:

没有答案