gpt4 book ai didi

r - 如何使用字符值连接/合并两个表?

转载 作者:行者123 更新时间:2023-12-04 12:01:51 24 4
gpt4 key购买 nike

我想根据名字、姓氏和年份组合两个表,并创建一个新的二进制变量,指示表 1 中的行是否存在于第二个表中。

第一张表是一个赛季NBA球员的一些属性的面板数据集:

   firstname<-c("Michael","Michael","Michael","Magic","Magic","Magic","Larry","Larry")
lastname<-c("Jordan","Jordan","Jordan","Johnson","Johnson","Johnson","Bird","Bird")
year<-c("1991","1992","1993","1991","1992","1993","1992","1992")

season<-data.frame(firstname,lastname,year)


firstname lastname year
1 Michael Jordan 1991
2 Michael Jordan 1992
3 Michael Jordan 1993
4 Magic Johnson 1991
5 Magic Johnson 1992
6 Magic Johnson 1993
7 Larry Bird 1992
8 Larry Bird 1992

第二个data.frame是入选全明星赛的NBA球员部分属性的面板数据集:
   firstname<-c("Michael","Michael","Michael","Magic","Magic","Magic")
lastname<-c("Jordan","Jordan","Jordan","Johnson","Johnson","Johnson")
year<-c("1991","1992","1993","1991","1992","1993")

ALLSTARS<-data.frame(firstname,lastname,year)



firstname lastname year
1 Michael Jordan 1991
2 Michael Jordan 1992
3 Michael Jordan 1993
4 Magic Johnson 1991
5 Magic Johnson 1992
6 Magic Johnson 1993

我想要的结果如下:
  firstname lastname    year    allstars

1 Michael Jordan 1991 1
2 Michael Jordan 1992 1
3 Michael Jordan 1993 1
4 Magic Johnson 1991 1
5 Magic Johnson 1992 1
6 Magic Johnson 1993 1
7 Larry Bird 1992 0
8 Larry Bird 1992 0

我尝试使用左连接。但不确定这是否有意义:
    test<-join(season, ALLSTARS, by =c("lastname","firstname","year") , type = "left", match = "all")

最佳答案

这是一个使用 data.table 的简单解决方案二元连接允许您在连接时通过引用更新列

library(data.table)
setkey(setDT(season), firstname, lastname, year)[ALLSTARS, allstars := 1L]
season
# firstname lastname year allstars
# 1: Larry Bird 1992 NA
# 2: Larry Bird 1992 NA
# 3: Magic Johnson 1991 1
# 4: Magic Johnson 1992 1
# 5: Magic Johnson 1993 1
# 6: Michael Jordan 1991 1
# 7: Michael Jordan 1992 1
# 8: Michael Jordan 1993 1

或使用 dplyr
library(dplyr)
ALLSTARS %>%
mutate(allstars = 1L) %>%
right_join(., season)
# firstname lastname year allstars
# 1 Michael Jordan 1991 1
# 2 Michael Jordan 1992 1
# 3 Michael Jordan 1993 1
# 4 Magic Johnson 1991 1
# 5 Magic Johnson 1992 1
# 6 Magic Johnson 1993 1
# 7 Larry Bird 1992 NA
# 8 Larry Bird 1992 NA

关于r - 如何使用字符值连接/合并两个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31317306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com