Here is a small piece of code creating three named vectors, all with the same three names of the elements:
# Create three similarly named vectors
vec_1 <- c("element_one" = 3, "element_two" = 7, "element_three" = 9)
vec_2 <- c("element_one" = 7, "element_two" = 2, "element_three" = 4)
vec_3 <- c("element_one" = 10, "element_two" = 16, "element_three" = 1)
I am looking to find a simple and elegant solution to the problem of combining multiple similarly named vectors using the bind_cols function, whilst maintaining those names as a new column in the final tibble. Up to this point I have used the base r cbind function to achieve this.
# Combined the three vectors by their names
combined_vec <- cbind(vec_1, vec_2 = vec_2[names(vec_1)], vec_3 = vec_3[names(vec_1)])
# Create a tibble that keeps the vector names as a column
new_combined <- combined_vec %>%
as.data.frame() %>%
rownames_to_column() %>%
tibble()
Is there a way of achieving this more simply, for example, with the bind_cols function? Or can anyone suggest a simpler solution to achieve this result?