0

I have a vector where there is a need to convert to dataframe. Is there a way to achieve this?

asd <- c("ABC\tCat + Catt1") 

Expected output

df ColA ColB ABC Cat ABC Catt1 

3 Answers 3

4

Where there is a will, there is a way

strsplit(asd, "\t") |> lapply(strsplit, " \\+ ") |> as.data.frame() |> setNames(c('ColA', 'ColB')) ColA ColB 1 ABC Cat 2 ABC Catt1 
Sign up to request clarification or add additional context in comments.

2 Comments

|> new syntax?
Yes, relatively new.
1

This is how you say... inelegant. But it works:

library(tidyverse) df = as.data.frame(str_split(unlist(str_split(asd, "\t")), "\\+ ")) %>% setnames(c("ColA", "ColB")) 
 ColA ColB 1 ABC Cat 2 ABC Catt1 

Comments

0
library(tidyverse) asd <- c("ABC\tCat + Catt1") asd %>% str_replace_all(regex("\\W+"), " ") %>% as_tibble() %>% separate(value, into = c("ColA", "A", "B"), sep = " ") %>% pivot_longer(-ColA, values_to = "ColB") %>% select(-name) # A tibble: 2 × 2 ColA ColB <chr> <chr> 1 ABC Cat 2 ABC Catt1 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.