基于不同数据帧中的值重新编码第二数据帧中的值

发布于 2025-01-20 18:36:17 字数 2288 浏览 5 评论 0原文

我想根据不同数据框中的相应值在第二个数据框中重新编码值。

例如,这是第一个数据框架的样子。每个代码都分配给相应的区域。

区域代码
112345
223456
223457
223458
345678
345679

如下

to_codefr_code
2345612345
23457
2345723456
第二
所示
数据45678

框架 看起来像这样:

to_codefrom_code
21
22
32
32
13
12

I want to recode the values in my second data frame based on the corresponding value in a different data frame.

for example, here's what the first data frame looks like. Each code is assigned to a corresponding zone.

zonecode
112345
223456
223457
223458
345678
345679

the second data frame looks like this:

to_codefr_code
2345612345
2345723456
4567823457
4567823458
1234545678
1234523457

but I want to recode it based on the corresponding zones, so it would look like this:

to_codefrom_code
21
22
32
32
13
12

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2025-01-27 18:36:17

我们可以使用匹配,从“ df1”中获取匹配的“代码”的索引,并在df2中的列值,并提取相应的“区域”

library(dplyr)
df3 <- df2 %>% 
   mutate(across(c(to_code, fr_code), ~ df1$zone[match(.x, df1$code)]))

base base r base中的索引。 >

df3 <- df2

df3[c("to_code", "fr_code")] <- lapply(df3[c("to_code", "fr_code")],
      function(x) df1$zone[match(x, df1$code)])

- 输出

df3
  to_code fr_code
1       2       1
2       2       2
3       3       2
4       3       2
5       1       3
6       1       2

数据

df1 <- structure(list(zone = c(1L, 2L, 2L, 2L, 3L, 3L), code = c(12345L, 
23456L, 23457L, 23458L, 45678L, 45679L)), class = "data.frame", row.names = c(NA, 
-6L))

df2 <- structure(list(to_code = c(23456L, 23457L, 45678L, 45678L, 12345L, 
12345L), fr_code = c(12345L, 23456L, 23457L, 23458L, 45678L, 
23457L)), class = "data.frame", row.names = c(NA, -6L))

We can use match to get the index of the matched 'code' from 'df1' with the values of the columns in df2 and extract the corresponding 'zone'

library(dplyr)
df3 <- df2 %>% 
   mutate(across(c(to_code, fr_code), ~ df1$zone[match(.x, df1$code)]))

Or in base R

df3 <- df2

df3[c("to_code", "fr_code")] <- lapply(df3[c("to_code", "fr_code")],
      function(x) df1$zone[match(x, df1$code)])

-output

df3
  to_code fr_code
1       2       1
2       2       2
3       3       2
4       3       2
5       1       3
6       1       2

data

df1 <- structure(list(zone = c(1L, 2L, 2L, 2L, 3L, 3L), code = c(12345L, 
23456L, 23457L, 23458L, 45678L, 45679L)), class = "data.frame", row.names = c(NA, 
-6L))

df2 <- structure(list(to_code = c(23456L, 23457L, 45678L, 45678L, 12345L, 
12345L), fr_code = c(12345L, 23456L, 23457L, 23458L, 45678L, 
23457L)), class = "data.frame", row.names = c(NA, -6L))
唱一曲作罢 2025-01-27 18:36:17

我们可以使用 match 尝试以下基本 R 代码

> df2[] <- with(df1, zone[match(unlist(df2), code)])
> df2
  to_code fr_code
1       2       1
2       2       2
3       3       2
4       3       2
5       1       3
6       1       2

We can try the following base R code using match

> df2[] <- with(df1, zone[match(unlist(df2), code)])
> df2
  to_code fr_code
1       2       1
2       2       2
3       3       2
4       3       2
5       1       3
6       1       2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文