Let's say I've downloaded a list from a random URL with the hhtr
function and gives a response
with three columns (salary, health...).
I have these four lines of code which I want to execute in the tidyverse environment.
dataframe_1 <- as.data.frame(response$salary)
dataframe_2 <- as.data.frame(response$health)
dataframe_3 <- as.data.frame(response$travel)
binded_df <- bind_cols(dataframe_1, dataframe_2, dataframe_3)
What I'm looking for is something like this:
binded_df %>%
"get the three columns in a single dataframe without creating three separate
df and then combine them"
Extra question:
And in a scenario that this code is in a function with unknown variables and only on variable per column. How would that look like?
CodePudding user response:
It'd be helpful if you provided some more information, but it looks like you might be looking for purrr::map_df
Here's an example:
library(purrr)
response <- list(
travel = c(1, 2, 1, 2, 1),
salary = c(30000, 40000, 25000, 70000, 90000),
health = c("A", "B", "C", "D", "E")
)
purrr::map_df(response, ~.x)
Which returns:
# A tibble: 5 × 3
travel salary health
<dbl> <dbl> <chr>
1 1 30000 A
2 2 40000 B
3 1 25000 C
4 2 70000 D
5 1 90000 E