简体   繁体   English

如何在 dataframe 中使用来自另一个 dataframe 的信息在 R 中添加不同长度的列?

[英]How to add a column in a dataframe using information from another dataframe with different lengths in R?

I have two dataframes with different columns and rows lengths.我有两个具有不同列和行长度的数据框。 I want to add two columns in the dataframe A from the dataframe B (columns X and Y) using the "region" column that is present in both dataframes.我想使用两个数据帧中都存在的“区域”列从 dataframe B(列 X 和 Y)的 dataframe A 中添加两列。 The figure shows how it looks like and how I would like to get (dataframe C, but can be add to dataframe A as well).该图显示了它的外观以及我希望如何获得(数据框 C,但也可以添加到 dataframe A 中)。 I am just showing you the head, but it has a lot of species and the match should be done for all species and the region with x and Y columns.我只是给你看头,但它有很多物种,应该对所有物种和带有 x 和 Y 列的区域进行匹配。 Could someone please help me?有人可以帮我吗? enter image description here在此处输入图像描述

You can use dplyr::left_join to add the columns based on region and then use select to select only the columns you need.您可以使用dplyr::left_join根据region添加列,然后仅使用select到 select 列。

library(dplyr)
dataframe_a %>%
  left_join(dataframe_b, by = "region") %>%
  select(species, region, X, Y)

There are a lot of ways to join two tables.有很多方法可以连接两个表。 I'm only showing you one, but you can find more material here我只给你看一个,但你可以在这里找到更多的材料

If you left_join you won`t get the desired output:如果您left_join ,您将无法获得所需的 output:

data.frame(species=dfA$species, region=dfA$region, X=dfB$X, Y=dfB$Y)
species <- (rep("Aa achalensis",6))
region <- c("ABT", "AFG", "AGE", "AGS", "AGW", "ALA")
dfA <- data.frame(species, region)

region <- c("ABT", "AGE", "AGS", "AGW", "ALA", "ALB")
LEVEL3_NAM <- c("Alberta", "Argentina Northeast", "Argentina South", "Argentina Northwest", "Alabama", "Albania")
LEVEL2_COD <- c(71, 85, 85, 85,78, 13)
LEVEL1_COD <- c(7,8,8,8,7,1)
X <- c(1,2,3,4,5,6)
Y <- c(7,8,9,10,11,12)
dfB <- data.frame(region, LEVEL3_NAM, LEVEL2_COD, LEVEL1_COD, X, Y)

left_join(dfA, dfB, by="region") %>% 
  select(species, region, X, Y)

data.frame(species=dfA$species, region=dfA$region, X=dfB$X, Y=dfB$Y)

With left_join使用left_join

       species region  X  Y
1 Aa achalensis    ABT  1  7
2 Aa achalensis    AFG NA NA
3 Aa achalensis    AGE  2  8
4 Aa achalensis    AGS  3  9
5 Aa achalensis    AGW  4 10
6 Aa achalensis    ALA  5 11

With data.frame(species=dfA$species, region=dfA$region, X=dfB$X, Y=dfB$Y)使用data.frame(species=dfA$species, region=dfA$region, X=dfB$X, Y=dfB$Y)

        species region X  Y
1 Aa achalensis    ABT 1  7
2 Aa achalensis    AFG 2  8
3 Aa achalensis    AGE 3  9
4 Aa achalensis    AGS 4 10
5 Aa achalensis    AGW 5 11
6 Aa achalensis    ALA 6 12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM