Hi everybody!
I have a data frame like this:
Df <- data.frame(A=c(2,3,9,12,2,5,7,7,1,23,3,4,14,3,9,8,6,11,9,4),B=c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2))
Df
and my code is:
#translates the column based on the counter in column B
Df %>%
mutate(B = if_else(B == 1, "A", "B")) %>%
group_by(B) %>%
mutate(var = paste0("V",row_number())) %>%
pivot_wider(id_cols = B, names_from = var, values_from = A) %>%
rename(row_name = B)
#creates a new data frame
new <- Df %>%
group_by(B) %>%
mutate(var = paste0("V",row_number())) %>%
pivot_wider(id_cols = B, names_from = var, values_from = A)
#extraction of the maximum value and saving in a new df
new$row_maximum = apply(new[,-1], 1, max)
complete <- new %>% rowwise() %>% mutate(Pos = which(c_across(V1:V10) == row_maximum )[1])
fin <- matrix(sample(c(0:0), 50, replace = TRUE), nrow(complete))
fin <- as.data.frame(fin)
#part to improve
tab_max <- new$row_maximum
tab_max <- as.data.frame(tab_max)
fin$V14 <- tab_max
my question is: is there any way to improve the last part? is it possible to do the last step using the position saved in complete$Pos?