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
?
CodePudding user response:
In base R:
stack(list(x = x, y = y, z= z))
values ind
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
in tidyverse:
library(tidyverse)
unnest(enframe(lst(x, y, z)), value)
# A tibble: 6 x 2
name value
<chr> <chr>
1 x My name is Andrea and I live in Vancouver
2 x I work at a university
3 y My name is Andrew and I live in New York
4 y I work at a hospital
5 z My name is Alessia and I live in Rome
6 z I work for the government
CodePudding user response:
Another tidyverse
option:
library(tidyverse)
lst(x, y, z) %>%
bind_rows() %>%
pivot_longer(everything()) %>%
arrange(name)
Or we could use map_df
from purrr
:
map_df(lst(x, y, z), ~ data.frame(value = .x), .id = "name")
Output
name value
<chr> <chr>
1 x My name is Andrea and I live in Vancouver
2 x I work at a university
3 y My name is Andrew and I live in New York
4 y I work at a hospital
5 z My name is Alessia and I live in Rome
6 z I work for the government