Hi again.
I made the dataset. But I am still getting errors. Appreciate the help.
library(tidyverse)
DF <- data.frame(V1 = c("a", "b", "c", "d;e", "c;d", "a;b","a;c;e", "b;c", "f", "g;h", "c;a;b;d", "a;f;c;d;e;g"),
V2 = c("a", "b;d", "f", "d;e", "d", "a;e;c", "f;a", "d", "f;g;a", "g;h", "a;d;c;e" , "f;a;d;h;j"))
MyFunc <- function(V1,V2) {
V1 <- str_split(V1,";")
V2 <- str_split(V2,";")
Matched_Type <- map2_dbl(V1,V2, ~case_when(
all(.x %in% .y) & all(.y %in% .x) ~ 1,
any(.x %in% .y) ~ 2,
TRUE ~ 0
))
Matched_Response <- map2_chr(V1,V2, ~paste(.x[.x %in% .y], collapse = ";"))
return(list(Type = Matched_Type, Response = Matched_Response,
MaxMatches = (max(nchar(Matched_Response))+1)/2))
}
OUT <- MyFunc(DF$Column_1,DF$Column_2)
#> Warning in max(nchar(Matched_Response)): no non-missing arguments to max;
#> returning -Inf
OUT
#> $Type
#> numeric(0)
#>
#> $Response
#> character(0)
#>
#> $MaxMatches
#> [1] -Inf
DF$Type <- OUT$Type
#> Error in `$<-.data.frame`(`*tmp*`, Type, value = numeric(0)): replacement has 0 rows, data has 12
DF$Response <- OUT$Response
#> Error in `$<-.data.frame`(`*tmp*`, Response, value = character(0)): replacement has 0 rows, data has 12
NewNames <- paste("Match",1:OUT$MaxMatches, sep = "_")
#> Error in 1:OUT$MaxMatches: result would be too long a vector
DF2 <- separate(DF,col = Response, into = NewNames,
remove = FALSE, fill = "right")
#> Error: object 'Response' not found
DF2
#> Error in eval(expr, envir, enclos): object 'DF2' not found
reprex::reprex()
#> x Install the styler package in order to use `style = TRUE`.
#> ℹ Non-interactive session, setting `html_preview = FALSE`.
#> CLIPR_ALLOW has not been set, so clipr will not run interactively
#> Error in switch(where, expr = stringify_expression(x_expr), clipboard = ingest_clipboard(), : EXPR must be a length 1 vector
Created on 2022-03-24 by the reprex package (v2.0.1)