I have data frame of multiple columns, each row will represent a condition that I want to use in another data frame to collect a score.
the first data frame looks like this:
condition_df <- data.frame(
type = c("3701", "4901", "3701"),
position = c(51, 22, 13),
subtype = c("M", "D", "E")
)
and the data that I want to apply the condition on is :
df <- data.frame(
id = 1:5,
type1 = c("3701", "4901", "4902", "4903", "1501"),
type2 = c("3801", "4901", "4901","5050","6069"),
type3 = c("3901", "5001", "8901","5050","6069"),
type4 = c("4001", "3901", "7901","5050","6069"),
51 = c("M", "A", "M", "M", "A"),
22 = c("D", "D", "K", "A", "D"),
13 = c("E", "G", "E", "E", "G"),
55 = c("M", "A", "M", "M", "A"),
25 = c("D", "D", "K", "A", "D"),
16 = c("E", "G", "E", "E", "G")
)
The idea is if the type(type1:type4) in df does not match type in condition_df, then look for the columns that there number equal to position in condition_df and then count how many letters in that column that match the subtype. Example is the first line of condition_df, if 3701 is not available in type1 to type4 , then count how many M at column 51. In that case the count will = 2 as we do not count the first M as it for that row the type1 matches type in condition_df.
I want to get the final score for each type in the condition_df, so for the type 3701, we have 2 results, one from column number 51 and one from column number 13, in that case I want to get the sum of these 2 results.
I’ve tried several ways but it did not work:
df <-df %>%
rowwise() %>%
mutate(
!!new_column_name := case_when(
!any(c_across(type1:type4) == condition_df$type) ~ sum(get(condition_df$position) == condition_df$type),
TRUE ~ 0))%>%
ungroup()
I’m sorry if my explanation is not clear enough.
Any help is greatly appreciated.
Regrads