1

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")
Jeremy K.
  • 1,710
  • 14
  • 35

2 Answers2

1

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)

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

We can use fread from data.table. Would be faster

library(data.table)
fread(text = t2, sep="=")
#   V1 V2
#1:  a 10
#2:  b  2
#3:  c 20
akrun
  • 874,273
  • 37
  • 540
  • 662