How do I split this into two columns?
I have a list here that contains
[[1]]
[1] "A" "O"
[[2]]
[1] "F" "O"
[[3]]
[1] "F" "O"
[[4]]
[1] "F" "OP"
Is it possible to do this?
I tried to do do split(., " ") but it didn't do anything
FJCC
May 4, 2022, 6:14pm
2
Here is one method.
MyList <- list(c("A","O"),c("F","O"),c("F","O"),c("F","OP"))
library(purrr)
map_dfr(MyList, ~set_names(.x, nm = c("V1", "V2")))
#> # A tibble: 4 x 2
#> V1 V2
#> <chr> <chr>
#> 1 A O
#> 2 F O
#> 3 F O
#> 4 F OP
Created on 2022-05-04 by the reprex package (v2.0.1)
1 Like
PeterHui:
[[1]]
[1] "A" "O"
[[2]]
[1] "F" "O"
[[3]]
[1] "F" "O"
[[4]]
[1] "F" "OP"
I'd like to provide you with another solution, though it by default turns the list into a matrix:
mx1 <- do.call(rbind,list(c("A","O"),c("F","O"),c("F","O"),c("F","OP")))
mx1
[,1] [,2]
[1,] "A" "O"
[2,] "F" "O"
[3,] "F" "O"
[4,] "F" "OP"
mx1[,1]
[1] "A" "F" "F" "F"
mx1[,2]
[1] "O" "O" "O" "OP"
I have no idea why this works but it seems to work, it knows where to split it somehow
system
Closed
May 12, 2022, 6:17pm
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.