【问题标题】:Removing a repeated value in a row删除一行中的重复值
【发布时间】:2020-11-24 16:25:36
【问题描述】:

我在数据框中有两列,其中可能有也可能没有复制值。如果第二列与第一列具有相同的值,我想将该值替换为 NULL 值或指示该值已被替换的字符串。如果值不同,我想保留这两个值。例如: 我要拿这个

col_1      col_2
a            a
a            b
b            d
c            c
c            d
c            c
a            a

然后把它变成:

col_1      col_2
a            NULL
a            b
b            d
c            NULL
c            d
c            NULL
a            NULL

我该怎么做?

【问题讨论】:

    标签: r string dataframe


    【解决方案1】:

    你也可以试试:

    #Code
    df$col_2 <- ifelse(df$col_2==df$col_1,'NULL',df$col_2)
    

    输出:

    df
      col_1 col_2
    1     a  NULL
    2     a     b
    3     b     d
    4     c  NULL
    5     c     d
    

    使用的一些数据:

    #Data
    df <- structure(list(col_1 = c("a", "a", "b", "c", "c"), col_2 = c("a", 
    "b", "d", "c", "d")), class = "data.frame", row.names = c(NA, 
    -5L))
    

    另一个选项可以是,使用正确的R sintax:

    #Code2
    df$col_2[df$col_2==df$col_1]<-'NULL'
    

    相同的输出。

    使用ifelse() 方法,我们得到:

    df
      col_1 col_2
    1     a  NULL
    2     a     b
    3     b     d
    4     c  NULL
    5     c     d
    6     c  NULL
    7     a  NULL
    

    【讨论】:

    • 如果 col_1 和 col_2 的组合重复,这似乎不起作用。我该如何更改您的答案以适应这种情况?
    • @Alokin ifelse() 方法可行,您能否提供您提到的问题的数据样本?
    • @Alokin 我已经对你的新数据执行了这两个代码,它们产生了你包含的输出。有什么问题?
    • 我认为那是我的错误。我弄乱了代码。你的回答有效,谢谢鸭子。
    • @Alokin 太棒了!非常感谢,也支持您的问题,因为它经过充分研究和定义:)
    【解决方案2】:

    通过 NULL 值,我假设您需要 NA,如果您需要实际的字符串 NULL,您可以使用 'NULL' 代替 NA_character_ ,如 Duck 的回答。

    library(dplyr)
    df %>% 
    mutate(col_2 = case_when(col_1 == col_2 ~ NA_character_, TRUE ~ col_2))
    # A tibble: 5 x 2
    # Rowwise: 
      col_1 col_2
      <chr> <chr>
    1 a     NA   
    2 a     b    
    3 b     d    
    4 c     NA   
    5 c     d    
    

    基于新的输入:

    df %>% mutate(col_2 = case_when(col_1 == col_2 ~ NA_character_, TRUE ~ col_2))
    # A tibble: 7 x 2
    # Rowwise: 
      col_1 col_2
      <chr> <chr>
    1 a     NA   
    2 a     b    
    3 b     d    
    4 c     NA   
    5 c     d    
    6 c     NA   
    7 a     NA   
    

    使用的数据:

    df
    # A tibble: 7 x 2
      col_1 col_2
      <chr> <chr>
    1 a     a    
    2 a     b    
    3 b     d    
    4 c     c    
    5 c     d    
    6 c     c    
    7 a     a    
    

    【讨论】:

    • 这只有在表格中没有重复的情况下才有效。如果某些对像这样重复,您会怎么做: col_1 col_2 1 a a 2 a b 3 b d 4 c c 5 c d 6a a
    • @Alokin,已更新我的答案,请检查是否相同。
    【解决方案3】:

    我们可以使用data.table方法,快速高效

    library(data.table)
    setDT(df)[col_1 == col_2, col_2 := 'NULL']
    

    -输出

    df
    #   col_1 col_2
    #1:     a  NULL
    #2:     a     b
    #3:     b     d
    #4:     c  NULL
    #5:     c     d
    

    数据

    df <- structure(list(col_1 = c("a", "a", "b", "c", "c"), col_2 = c("a", 
    "b", "d", "c", "d")), class = "data.frame", row.names = c(NA, 
    -5L))
    

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      相关资源
      最近更新 更多