使用R使用代码匹配两个数据帧



我有两个数据框架。df1:

鱼类tbody> <<tr>
年度渔获量
1012500
1011759
116111
139178
library(dplyr)
df1 <-
structure(list(Species = c(101L, 101L, 116L, 139L), Catch.by.Year = c(2500L, 
1759L, 111L, 178L)), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))
df2 <-
structure(list(code = c(101L, 116L, 139L, 144L), name = c("COD", 
"YELLOWTAIL", "BLUE ANTIMORA", "CUSK")), row.names = c(NA, -4L
), class = c("tbl_df", "tbl", "data.frame"))
df1 %>% 
inner_join(df2,by = c("Species" = "code")) %>% 
select(-Species) %>% 
select(Species = name,Catch.by.Year)
# A tibble: 4 x 2
Species       Catch.by.Year
<chr>                 <int>
1 COD                    2500
2 COD                    1759
3 YELLOWTAIL              111
4 BLUE ANTIMORA           178

最新更新