I have a (large) list of dataframes each with a single column. I would like to split these into 20 columns by a delimiter. On a single dataframe:
file2=file1 %>%
separate(V1,into=c(paste0("column_",letters[1:20])),sep="\\|")
Is there a way to apply this to the entire list? I have tried:
map(filelist, ~ separate(.x, into=c(paste0("column_",letters[1:20])),sep="\\|"))
Which returned:
Caused by error in `separate()`:
! `col` is absent but must be supplied.
Alternatively, is something other than separate() that would be more efficient?
No data was provided, so I'll create a minimal dataset that I think resembles the OP's and gives the desired output.
library(tidyverse)
filelist <- list(data1=tibble(V1=c("1|2|3|4|5", "6|7|8|9|10")),
data2=tibble(V1=c("6|7|8|9|10", "11|12|13|14|15")),
data3=tibble(V1=c("16|17|18|19|20", "21|22|23|24|25")))>
filelist
$data1
# A tibble: 2 × 1
V1
<chr>
1 1|2|3|4|5
2 6|7|8|9|10
$data2
# A tibble: 2 × 1
V1
<chr>
1 6|7|8|9|10
2 11|12|13|14|15
$data3
# A tibble: 2 × 1
V1
<chr>
1 16|17|18|19|20
2 21|22|23|24|25
Either of the two commands below should give the desired output:
map(filelist, ~ separate(.x,
cols=V1,
into=c(paste0("column_", letters[1:5])),
sep="\\|"))
map(filelist, ~ separate_wider_delim(.x,
cols=V1,
names=c(paste0("column_", letters[1:5])),
delim="|"))
$data1
# A tibble: 2 × 5
column_a column_b column_c column_d column_e
<chr> <chr> <chr> <chr> <chr>
1 1 2 3 4 5
2 6 7 8 9 10
$data2
# A tibble: 2 × 5
column_a column_b column_c column_d column_e
<chr> <chr> <chr> <chr> <chr>
1 6 7 8 9 10
2 11 12 13 14 15
$data3
# A tibble: 2 × 5
column_a column_b column_c column_d column_e
<chr> <chr> <chr> <chr> <chr>
1 16 17 18 19 20
2 21 22 23 24 25