Search code examples
rtidyversereadr

parse chr string into a dataframe using delimiters


My data is in a format, that looks like this:

t2 <- "a = 10\n   b = 2\n   c =20\n"

How can I parse it so that I get a dataframe that looks like this?

# a 10
# b 2
# c 20

I'm trying to use readr::format_delim(), but I'm happy to use any solution. The code below doesn't give me the right answer.

library(readr)
t2 <- as_tibble(t2)
format_delim(t2, delim = "\n")

Solution

  • Use read.table or the likes with sep argument.

    read.table(text = t2, sep = "=")
    
    #     V1 V2
    #1    a  10
    #2    b   2
    #3    c  20
    

    Or read.csv(text = t2, sep = "=", header = FALSE)