I have a group of variables I created in my R environment:
x <- c("My name is Andrea and I live in Vancouver",
"I work at a university")
y <- c("My name is Andrew and I live in New York",
"I work at a hospital")
z <- c("My name is Alessia and I live in Rome",
"I work for the government")
I want to convert these "character" variables into tibbles and assign as a variable to each tibble the name of the dataset (so in this case, the names would be x, y and z).
Example of the tibble I'd like:
# A tibble: 6 × 2
value name
<chr> <chr>
1 My name is Andrea and I live in Vancouver "x"
2 I work at a university "x"
3 My name is Andrew and I live in New York "y"
4 I work at a hospital "y"
5 My name is Alessia and I live in Rome "z"
6 I work for the government "z"
Now the code here: test <- as_tibble(c(x,y,z)) %>% mutate(name = c("x", "y", "z")) doesn't work because of a mismatch in sizes.
This code does, but isn't in "dplyr" format:
xyz.list <- Hmisc::llist(x, y ,z)
df <- do.call(rbind, unname(Map(cbind, source = names(xyz.list), xyz.list)))
df <- df %>% as_tibble()
My question: is there a way to "merge" or combine different datasets AND create a column with the name of the original merged dataset using dplyr?