Replace space in data.frame with new column/vector

i have a data.frame that looks like this

year <- c(2016,2017,2018)
a <- c("1 2 3 4 5", "6 7 8 9 10", "11 12 13 14 15")
df <- data.frame(year, a)

shows this:

  year      a
1 2016      1 2 3 4 5
2 2017      6 7 8 9 10
3 2018      11 12 13 14 15

i want to replace the "space" between the numbers with a new column/vector in the dataframe that it looks like this:

  year column1 column2 column3 column4 column5
1 2016       1       2       3       4       5
2 2017       6       7       8       9      10
3 2018      11      12      13      14      15
# Result should be
column1 <- c(1,6,11)
column2 <- c(2,7,12)
column3 <- c(3,8,13)
column4 <- c(4,9,14)
column5 <- c(5,10,15)
dfNEW <- data.frame(year, column1, column2, column3, column4, column5)

but of course because i have a large data.frame and it's not possible to do it manually... do you have any idea how to convert it and replace the space with a new column?

thank you very much

1 Like
library(tidyr)

df %>% separate(a, paste0("column", 1:5), " ")

5 Likes

you're the best, that's the solution, simple and easy, thank you very much for the quick anser