【问题标题】:Matching values in two columns then based on that returning new value in R然后基于 R 中返回的新值匹配两列中的值
【发布时间】:2019-10-28 19:03:15
【问题描述】:

我在数据框中有这些列,如下所示:

combination  color_1  color_2

1_1          red       red
1_2          red       blue
1_3          red       green
1_4          red       yellow
2_1          blue      red
2_2          blue      blue
2_3          blue      green
2_4          blue      yellow
... 

基于匹配 color_1 和 color_2 值,我希望能够创建输出匹配结果的新列。 对此有一定的规范。对于“red”和“red”相同的第一行,新列中的输出(例如“Red-Only”)应该是“1” ,然后每隔一个匹配“2”。然后,我将重复此代码,然后选择出现“蓝色”和“蓝色”的匹配项,在下一列中输出“1”(例如“Blue-Only”),在其他任何地方输出“2”。这适用于仅黄色匹配、仅绿色匹配等。所以最后我会根据条件有 4 个额外的列。

提前感谢您的帮助!

【问题讨论】:

  • color_1列中,颜色总是分组吗?

标签: r loops match


【解决方案1】:

让我们从您现有的数据开始:

df <- structure(list(combination = c("1_1", "1_2", "1_3", "1_4", "2_1", 
"2_2", "2_3", "2_4"), color_1 = c("red", "red", "red", "red", 
"blue", "blue", "blue", "blue"), color_2 = c("red", "blue", "green", 
"yellow", "red", "blue", "green", "yellow")), class = "data.frame", row.names = c(NA, 
-8L))

  combination color_1 color_2
1         1_1     red     red
2         1_2     red    blue
3         1_3     red   green
4         1_4     red  yellow
5         2_1    blue     red
6         2_2    blue    blue
7         2_3    blue   green
8         2_4    blue  yellow

一种解决方案是遍历您的四个颜色类别,检查是否匹配。

colors <- c('red', 'green', 'yellow', 'blue')

matches <- lapply(colors, function(x) {
  out <- ifelse(with(df, color_1 == color_2 & color_1 == x), 1, 2)
  out
})

然后用你想要的列名命名这个操作的结果。

names(matches) <- paste(colors, 'only', sep = '_')

最后,将结果与原始数据粘合在一起:

df.new <- cbind(df, as.data.frame(matches))

  combination color_1 color_2 red_only green_only yellow_only blue_only
1         1_1     red     red        1          2           2         2
2         1_2     red    blue        2          2           2         2
3         1_3     red   green        2          2           2         2
4         1_4     red  yellow        2          2           2         2
5         2_1    blue     red        2          2           2         2
6         2_2    blue    blue        2          2           2         1
7         2_3    blue   green        2          2           2         2
8         2_4    blue  yellow        2          2           2         2

【讨论】:

    【解决方案2】:

    你可以使用 ifelse。如果你有很多循环将是一个好主意

    cols <- data.frame(
    color_1=c("Red","Red","Red","Red","Blue","Blue","Blue","Blue"),
    color_2=c("Red","Blue","Green","Yellow","Red","Blue","Green","Yellow")
    ) 
    
    cols$redonly <- ifelse( cols$color_1 %in% "Red" & cols$color_2 %in% "Red" , 1 ,2 )
    cols$Blueonly <- ifelse( cols$color_1 %in% "Blue" & cols$color_2 %in% "Blue" , 1 ,2 )
    cols$greeonly <- ifelse( cols$color_1 %in% "Green" & cols$color_2 %in% "Green" , 1 ,2 )
    

    【讨论】:

      【解决方案3】:

      这是一种不依赖于知道颜色名称的方法。

      fun <- function(color, DF, col1, col2){
        2L - (color == DF[[col1]] & color == DF[[col2]])
      }
      
      cols1 <- unique(df1$color_1)
      cbind(df1, sapply(cols1, fun, df1, 'color_1', 'color_2'))
      #  combination color_1 color_2 red blue
      #1         1_1     red     red   1    2
      #2         1_2     red    blue   2    2
      #3         1_3     red   green   2    2
      #4         1_4     red  yellow   2    2
      #5         2_1    blue     red   2    2
      #6         2_2    blue    blue   2    1
      #7         2_3    blue   green   2    2
      #8         2_4    blue  yellow   2    2
      

      数据。

      df1 <- read.table(text = "
      combination  color_1  color_2
      1_1          red       red
      1_2          red       blue
      1_3          red       green
      1_4          red       yellow
      2_1          blue      red
      2_2          blue      blue
      2_3          blue      green
      2_4          blue      yellow
      ", header = TRUE, stringsAsFactors = FALSE)
      

      【讨论】:

        猜你喜欢
        • 2022-06-30
        • 1970-01-01
        • 2016-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多