gpt4 book ai didi

r - 数据帧 R 之间的映射值

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

让我们创建示例数据:

df <- data.frame(date=c("2017-01-01","2017-01-02", "2017-01-03", "2017-01-04", "2017-01-05"), X1=c("A", "B", "C", "D", "F"),
X2=c("B", "A", "D", "F", "C"))
df2 <- data.frame(date=c("2017-01-01","2017-01-02", "2017-01-03", "2017-01-04", "2017-01-05"),
A=c("3", "4", "2", "1", "5"),
B=c("6", "2", "5", "1", "1"),
C=c("1", "4", "5", "2", "3"),
D=c("67", "67", "63", "61", "62"),
F=c("31", "33", "35", "31", "38"))

所以我有两个数据框,我想按日期以及 X1 和 X2 匹配从 df2 到 df 的值,并为它们创建新变量。对我来说这很棘手的是 df2 中的匹配值位于 colnames 中。最终结果应如下所示:

> result
date X1 X2 Var1 Var2
1 2017-01-01 A B 3 6
2 2017-01-02 B A 2 4
3 2017-01-03 C D 5 63
4 2017-01-04 D F 61 31
5 2017-01-05 F C 38 3

result <- data.frame(date=c("2017-01-01","2017-01-02", "2017-01-03", "2017-01-04", "2017-01-05"),
X1=c("A", "B", "C", "D", "F"),
X2=c("B", "A", "D", "F", "C"),
Var1=c("3", "2", "5", "61", "38"),
Var2=c("6", "4", "63", "31", "3"))

我想使用映射值,但无法弄清楚。第二个想法是使用 df2 进行长格式(融化),然后尝试,但也失败了。

好吧,这是我最好的尝试,只是觉得如果您必须为数据框创建多个(> 50)新变量,可能会有更有效的方法。

df2.long <- melt(df2, id.vars = c("date"))

df$Var1 <- na.omit(merge(df, df2.long, by.x = c("date", "X1"), by.y = c("date", "variable"), all.x = FALSE, all.y = TRUE))[,4]
df$Var2 <- na.omit(merge(df, df2.long, by.x = c("date", "X2"), by.y = c("date", "variable"), all.x = FALSE, all.y = TRUE))[,5]

最佳答案

使用dplyrtidyr:

df2_m <- group_by(df2, date) %>% 
gather('X1', 'var', -date)

left_join(df, df2_m) %>%
left_join(df2_m, by = c('date', 'X2' = 'X1')) %>%
rename(Var1 = var.x, Var2 = var.y) -> result

关于r - 数据帧 R 之间的映射值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42276930/

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