简体   繁体   English

基于其他列 R 的因素重命名级别

[英]Renaming levels based on factors of other column R

I have a large dataframe where names (col1) should be nested within location name (col2).我有一个大数据框,其中名称(col1)应该嵌套在位置名称(col2)中。 In the example below "d" from col1 should be within "z" of col2 but is listed as "y" in the eighth element.在下面的示例中,来自 col1 的“d”应该在 col2 的“z”内,但在第八个元素中列为“y”。 I cannot just rename "y" in col2 as it is correct in most places.我不能只是在 col2 中重命名“y”,因为它在大多数地方都是正确的。 I need to rename "y" to "z" only when col1 == "d".只有当 col1 == "d" 时,我才需要将 "y" 重命名为 "z"。 It is a large dataframe with multiple example so it is also not possible just to rename the element这是一个包含多个示例的大型数据框,因此也无法仅重命名元素

col1<-c("a","b","c","d","a","b","c","d")
col2<-c("x","y","z","z","x","y","z","y")
df<-data.frame(col1,col2)

This would be easy if you can create a data frame lookup with the correct combination of col1 and col2 .如果您可以使用col1col2的正确组合创建数据框lookup ,这将很容易。 You can then just left_join your original data frame with the lookup .然后,你可以left_join与原来的数据帧lookup

library(dplyr)

# Create a lookup table. In reality you probably need to create this with other methods.
lookup <- df %>%
  distinct() %>%
  filter(!(col1 %in% "d" & col2 %in% "y"))

# join col1 to the lookup
df2 <- df %>%
  select(-col2) %>%
  left_join(lookup, by = "col1")
df2
#   col1 col2
# 1    a    x
# 2    b    y
# 3    c    z
# 4    d    z
# 5    a    x
# 6    b    y
# 7    c    z
# 8    d    z

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM