简体   繁体   English

根据 R 中的另一个匹配 dataframe 重命名列值

[英]Rename column value based on another matching dataframe in R

Suppose that we have three dataframes dfa假设我们有三个数据框 dfa

name姓名 id ID
jack杰克 1 1个
rose玫瑰 2 2个

dfb dfb

id ID job工作
a A student学生
b b tutor导师

dfc dfc

primary基本的 colname列名
a A 2 2个
b b 1 1个

how could I rename the dfa id column from 1,2 to b,a using R?如何使用 R 将 dfa id 列从 1,2 重命名为 b,a?

Please help me solve this question!请帮我解决这个问题! Thank you so much!太感谢了!

One dplyr approach would be to use recode like so:一种dplyr方法是像这样使用recode

library(dplyr)

dfa |> 
  mutate(id = recode(id, !!!setNames(dfc$primary, dfc$colname)))
#>   name id
#> 1 jack  b
#> 2 rose  a

or a second option would be to use a left_join :或者第二种选择是使用left_join

dfa |> 
  left_join(dfc, by = c("id" = "colname")) |> 
  select(name, id = primary)
#>   name id
#> 1 jack  b
#> 2 rose  a

DATA数据

dfa <- data.frame(
              name = c("jack", "rose"),
                id = c(1L, 2L)
)

dfc <- data.frame(
           primary = c("a", "b"),
           colname = c(2L, 1L)
)

I haven't understood if the relationship between all three datasets is like this: dfa$id is equal to dfc$colname, and dfc$primary is equal to dfb$id, but if so, here's a possible answer:我一直不明白这三个数据集之间的关系是否是这样的:dfa$id 等于 dfc$colname,dfc$primary 等于 dfb$id,但如果是这样,这里有一个可能的答案:

# Load the dataframes
dfa <- data.frame(name = c("jack", "rose"),
                  id = c(1, 2))
dfb <- data.frame(id = c("a", "b"),
                  job = c("student", "tutor"))
dfc <- data.frame(primary = c("a", "b"),
                  colname = c(2, 1))

# Obtain the indexes that match between dfa and dfc
index <- match(dfa$id, dfc$colname)

# Set the id from dfa as the id in dfb taking into account the previous indices
dfa$id <- dfb$id[index]

# Print results
dfa

  name id
1 jack  b
2 rose  a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个数据帧中的匹配条件将列添加到 R 中的数据帧 - Adding column to a dataframe in R based on matching conditions in another dataframe 基于另一个在 dataframe 中创建新列,并与 R 中的另一个数据集匹配 - Create new column in dataframe based on another and matching to another dataset in R 在 R dplyr 中,根据其在另一列中的值重命名一个值 - In R dplyr, rename a value based on its value in another column 根据 R 中的列值,基于现有 dataframe 创建另一个 dataframe - Create another dataframe based on an existing dataframe based on a column value in R 基于 R 中的另一个 DataFrame 重命名列 - Rename columns based on another DataFrame in R 根据R中另一列中的值为“匹配ID”添加计数器 - Add counter for Matching ID based on value in another column in R 基于另一个数据帧重命名数据帧的列,除了 R 中不在该数据帧中的列 - Rename columns of a dataframe based on another dataframe except columns not in that dataframe in R 根据R中另一个数据框中的值重命名数据框列 - Rename dataframe columns based on values in another dataframe in R 根据R中另一列的行值将数据帧设置为向量 - subsetting a dataframe into a vector based upon a row value of another column in R 根据 dataframe R 中另一列中的值复制值 - Replicate value based on values in another column in dataframe R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM