【问题标题】:How to update rownames in R?如何更新 R 中的行名?
【发布时间】:2021-07-12 10:19:00
【问题描述】:

我有 3 个数据框,每个数据框包含三个变量和玩家的名字

a <- rnorm(16, 3, 2)
b <- rnorm(16, 1, 3)
c <- rpois(16, 3)
year <- c(rep(2015, 5), rep(2016, 5), rep(2017, 6))
player <- c("Alex", "CT", "Bill", "Brian", "Collin", "Chez", "Adam", "Danny III", "Lee", "Chris",
            "Erik", "Axel", "Louis", "Justin", "Dustin", "Johnson")

df_1 <- data.frame(player, year, a, b, c)

d <- rnorm(16, 3, 2)
e <- rnorm(16, 1, 3)
f <- rpois(16, 3)
year <- c(rep(2015, 5), rep(2016, 5), rep(2017, 6))
player <- c("Alexander", "C.T.", "Bill", "Brian", "Collin", "Chez", "Adam", "Danny IV", "Lee", "Chris",
            "Erik", "Axel", "Louis", "Justin", "Dustin", "Johnson")

df_2 <- data.frame(player, year, d, e, f)

g <- rnorm(16, 3, 2)
h <- rnorm(16, 1, 3)
i <- rpois(16, 3)
year <- c(rep(2015, 5), rep(2016, 5), rep(2017, 6))
player <- c("Alex", "CT", "Bill", "Brian", "Collin", "Chez", "Adam", "Danny III", "Lee", "Chris",
            "Erik", "Axel", "Louis", "Justin", "Dustin", "Johnson")

df_3 <- data.frame(player, year, g, h, i)

这个数据框包含了每个变量数据集对应的玩家名字。 例如,从 d 到 f 的变量,Alex 与 Alexander 相同,从 g 到 i 的观测值与 Alex 相同。 Danny III 在 d 到 f 的变量中被命名为 Danny IV,在 g 到 i 的变量中被命名为 Danny III。

a_to_c <- c("Alex", "CT", "Danny III")
d_to_f <- c("Alexander", "C.T.", "Danny IV")
g_to_i <- c("Alex", "CT", "Danny III")

names_palyer <- data.frame(a_to_c, d_to_f, g_to_i)

我想将三个数据框按年份和播放器合并为一个数据框。我需要使用“names_player”中的信息将播放器与数据正确匹配 为了简单起见,我做了这个例子,实际上,我有数千个观察结果,所以我需要找到一种方法来自动匹配玩家的名字,这样我就可以拥有一个包含三个数据帧信息的数据帧。

【问题讨论】:

  • 感谢您指出这一点。我更新了代码。
  • 事实上,我希望有一个使用 df_1 作为基线的左连接

标签: r dplyr merge


【解决方案1】:

将输出('out')初始化为第一个数据('df_1')。循环遍历“names_palyer”列的索引(不包括最后一列),get 对应的“df_”对象的值(递增 1 - i + 1 - 假设对象命名为 df_1、df_2 等。 ),然后选择 'names_palyer' 列的子集(keydat),使用match 获取与 'tmp' 数据的 'player' 列匹配值的索引。将 'player' 替换为'keydat' 基于索引,然后执行merge(左连接-all.x = TRUE),最后,将match 到keydat 列的输出'player' 更改为'keydat' 的第二列值(所以对下一次迭代很有用)

out <- df_1
for(i in 1:(ncol(names_palyer)-1)) {
     tmp <- data.table::copy(get(paste0('df_', i + 1)))
     keydat <- names_palyer[c(i, i + 1)]
     keydat <- keydat[keydat[[2]] %in% tmp$player,, drop = FALSE]
     i1 <- match(keydat[[2]], tmp$player, nomatch = 0) 
     
     tmp$player[i1] <- keydat[[1]]
     print(tmp)
     out <- merge(out, tmp, by = c('player', 'year'), all.x = TRUE)
     i2 <- match(keydat[[1]], out$player, nomatch = 0)
     
     out$player[i2] <- keydat[[2]] 
}

-输出

out
      player year           a           b c         d           e f          g           h i
1       Adam 2016  0.03587367 -0.57907496 3 5.1149009  2.47064240 2  2.3325348  0.62526907 6
2       Alex 2015  1.27778013  0.05809471 0 4.1932959  4.37934704 0  4.3226737 -0.33523019 5
3       Axel 2017  2.56466723  0.43108713 2 5.9970138 -2.19947169 4  0.9717511  2.05843957 3
4       Bill 2015  2.05594607  3.96167974 3 2.5232810  3.87191286 3  3.1726895  3.43683108 0
5      Brian 2015  3.44690732  0.35032810 4 4.7287671  0.08108714 2  2.8519495 -0.08249603 2
6         CT 2015  5.85679299 -1.57623304 2 3.9653678  1.68389034 3  3.0328709  1.04315644 2
7       Chez 2016  0.73604605 -2.58101736 1 4.0642894  0.04941299 3  5.4688474 -1.82831432 3
8      Chris 2016  0.95621081  2.05206411 4 2.7249987  2.42911270 8 -0.4515070 -2.12097504 0
9     Collin 2015  7.14194691  0.74030236 5 4.7879545  5.41397214 4  1.4835656  0.92897125 2
10 Danny III 2016  4.59832890  0.60355092 5 4.4822495  4.15865653 0  2.4950848  3.31059942 3
11    Dustin 2017  0.26640646 -0.23381080 4 5.3164916  3.67001803 1  0.7011976  2.59135173 4
12      Erik 2017  0.27363760 -4.50110125 3 4.9495033  3.31417537 3  4.1907692  5.57914934 6
13   Johnson 2017  7.12013083  2.52775367 3 1.9192381  4.33916287 2  3.3836699 -2.37444447 3
14    Justin 2017  3.41710305 -3.82843506 4 5.5590782  0.56030426 1  0.1670448  5.99934712 6
15       Lee 2016 -1.02002976 -3.24576311 4 0.9538381 -0.91783716 5  2.5668076 -0.67247680 2
16     Louis 2017  1.94420093  0.47369179 3 2.8249960 -1.28630731 7  3.0070664  1.25132019 5

随着OP的新数据

out <- copy(df_1)
for(i in 1:(ncol(names_palyer)-1)) {
     tmp <- data.table::copy(get(paste0('df_', i + 1)))
     keydat <- names_palyer[c(i, i + 1)]
     keydat <- keydat[keydat[[2]] %in% tmp$player,, drop = FALSE]
     i1 <- match(keydat[[2]], tmp$player, nomatch = 0) 
     
     tmp$player[i1] <- keydat[[1]]
     print(tmp)
     out <- merge(out, tmp, by = c('player', 'year'), all.x = TRUE)
     i2 <- match(keydat[[1]], out$player, nomatch = 0)
     
     out$player[i2] <- keydat[[2]][keydat[[1]] %in% out$player] 
}

library(dplyr)
library(purrr)
split.default(out[-(1:2)], sub("\\..*", "", names(out)[-(1:2)])) %>%
   map_dfc(reduce, coalesce)  %>% 
     bind_cols( out[1:2], .)

【讨论】:

  • 感谢您指出这一点。我更新我的代码。是的,我需要按年份和名称合并。你能解释一下你是如何让 R 认识到对于 df_1 我们应该使用 names_player 数据框的列“a_to_c”,对于 df_2 我们应该使用列“d_to_f”谢谢你的帮助
  • @PastorSoto 您的 names_palyer 数据列的顺序与 df_1', 'df_2', .... 因此,我们只是循环列序列
  • 当我在示例数据中执行此操作时,它运行良好,但是当我在具有更多变量且每个 df_ 具有不同数量的变量的实际数据中执行此操作时,它会发出此警告消息。你觉得有问题吗?警告消息:1:在 tmp$player[i1]
  • @PastorSoto 如果你能展示一个说明问题的例子,那就太好了。
  • @PastorSoto 我理解你的问题。可能您的 keydat 或 names_palyer 这些列没有像输入示例中那样匹配所有值。如果您只有较少的匹配项,最好创建 list 而不是 data.frame 作为 names_palyer
猜你喜欢
  • 2020-11-05
  • 1970-01-01
  • 2020-01-05
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多