MayaGans
MayaGans

Reputation: 1845

Split string into rows and columns

I have one long enormous string:

mystr <- "foo   one   undefined + foo   two   undefined + BAR   three   undefined + "

And I want to turn it into

   x1    x2        x3
1 foo   one undefined
2 foo   two undefined
3 bar three undefined

By using the + to create new rows, then spaces to create columns. Is this possible? I tried using str_split and mutate but I couldn't seem to figure out how to create new rows. Any help appreciated!

Upvotes: 2

Views: 65

Answers (2)

hello_friend
hello_friend

Reputation: 5788

Another Base R solution:

data.frame(do.call("rbind", sapply(strsplit(trimws(mystr, "both"), "\\+"), 
        function(x){strsplit(trimws(x, "both"), "\\s+")})))

Upvotes: 1

akrun
akrun

Reputation: 887213

We can use read.table after replacing the + with \n using gsub in base R

read.table(text = gsub("+", "\n", mystr, fixed = TRUE),
       header = FALSE, col.names = paste0('x', 1:3))
#    x1    x2        x3
#1 foo   one undefined
#2 foo   two undefined
#3 BAR three undefined

Or using strsplit with read.table

read.table(text = strsplit(mystr, " + ", fixed = TRUE)[[1]], header = FALSE)

Or we can use fread

library(data.table)
fread(text = gsub("+", "\n", mystr, fixed = TRUE), header = FALSE)

Or using tidyverse

library(dplyr)
library(tidyr)
tibble(col1 = mystr) %>% 
   separate_rows(col1, sep="\\s*\\+\\s*") %>%
   separate(col1, into = c('x1', 'x2', 'x3')) %>%
   na.omit
# A tibble: 3 x 3
#  x1    x2    x3       
#  <chr> <chr> <chr>    
#1 foo   one   undefined
#2 foo   two   undefined
#3 BAR   three undefined

Upvotes: 4

Related Questions