然后匹配两列中的值,然后基于R中返回的新值

问题描述 投票:0回答:1

我在数据框中的这些列看起来像:

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的值,我希望能够创建输出匹配结果的新列。 对此有某些规定。对于“红色”和“红色”相同的第一行,新列中的输出(例如“仅红色”)应为“ 1”,然后每隔两个匹配项为“ 2”。然后,我将重复此代码,但接着选择出现“蓝色”和“蓝色”的匹配项,以在下一列(例如“仅蓝色”)中输出“ 1”,并在其他任何地方输出“ 2”。这适用于仅黄色的匹配,仅绿色的匹配等。因此,根据条件,最后我将有4个额外的列。

感谢您的提前帮助!

r loops match
1个回答
0
投票

您可以使用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 )

0
投票

使用dplyr,我们可以使用case_when

library(tidyverse)


df %>% 
  mutate(test = case_when(
    color_1 == "red" & color_2 == "red" ~ "red-only",
    color_1 == "blue" & color_2 == "blue" ~ "blue-only",
    TRUE ~ "mixed"
  ))
#>   combination color_1 color_2      test
#> 1         1_1     red     red  red-only
#> 2         1_2     red    blue     mixed
#> 3         1_3     red   green     mixed
#> 4         1_4     red  yellow     mixed
#> 5         2_1    blue     red     mixed
#> 6         2_2    blue    blue blue-only
#> 7         2_3    blue   green     mixed
#> 8         2_4    blue  yellow     mixed

数据

df <- read.csv(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)
© www.soinside.com 2019 - 2024. All rights reserved.