I have created two dataframes, with df.1 containing my main data.
ID A_ratio B_ratio C_ratio
1 0.9 7.6 3.5
2 3.1 4.4 0.7
3 6.3 8.2 1.2
The dataframe cut only contains one row.
A_cut B_cut C_cut
4.5 5.3 2.0
I now want to use the values stored in cut to binarize df, turning X_ratio <= X_cut to 1 and X_ratio > X_cut to 0. The new column could be called X_bin. I've tried the following dplyr approach:
df.2 <- df.1 %>%
mutate(across(ends_with("ratio"), ~if_else(. <= get(cut[str_replace(cur_column(),"ratio","cut")]), 1, 0)
.names = "{.col}_bin"))%>%
rename_with(~str_replace(.,"_ratio",""),contains("_ratio_"))
select(ID, ends_with("bin"))
But I'm unfortunately getting an Error: unexpected symbol. Could someone point out my mistake? The desired output in df.2 would be
ID A_bin B_bin C_bin
1 1 0 0
2 1 1 1
3 0 0 1
Thanks a lot in advance!