基于不同数据帧中的值重新编码第二数据帧中的值
我想根据不同数据框中的相应值在第二个数据框中重新编码值。
例如,这是第一个数据框架的样子。每个代码都分配给相应的区域。
区域 | 代码 |
---|---|
1 | 12345 |
2 | 23456 |
2 | 23457 |
2 | 23458 |
3 | 45678 |
3 | 45679 |
如下
to_code | fr_code |
---|---|
23456 | 12345 |
23457 | |
23457 | 23456 |
第二 | 个 |
: | 所示 |
数据 | 45678 |
框架 看起来像这样:
to_code | from_code |
---|---|
2 | 1 |
2 | 2 |
3 | 2 |
3 | 2 |
1 | 3 |
1 | 2 |
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.
zone | code |
---|---|
1 | 12345 |
2 | 23456 |
2 | 23457 |
2 | 23458 |
3 | 45678 |
3 | 45679 |
the second data frame looks like this:
to_code | fr_code |
---|---|
23456 | 12345 |
23457 | 23456 |
45678 | 23457 |
45678 | 23458 |
12345 | 45678 |
12345 | 23457 |
but I want to recode it based on the corresponding zones, so it would look like this:
to_code | from_code |
---|---|
2 | 1 |
2 | 2 |
3 | 2 |
3 | 2 |
1 | 3 |
1 | 2 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们可以使用
匹配
,从“ df1”中获取匹配的“代码”的索引,并在df2中的列值,并提取相应的“区域”或
base base r base中的索引。 >
- 输出
数据
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'Or in
base R
-output
data
我们可以使用
match
尝试以下基本 R 代码We can try the following base R code using
match