Reputation: 6994
While debugging code in R, I want to create a dataframe from a string like
"Column_A|Column_B
Val-1|Val-2
Val-3|Val-4"
I remember seeing a piece of code somewhere that does something like:
df <- some_function("input string")
but can't seem to remember the syntax
I do not want to take the approach of creating two separate vectors and create a dataframe from it like:
column_a <- c("Val-1", "Val-2")
column_b <- c("Val-3", "Val-4")
df <- data.frame(column_a = column_a, column_b = column_b)
Upvotes: 1
Views: 227
Reputation: 389135
Alternatively, we can also use read.csv
df <- read.csv(text = "Column_A|Column_B
Val-1|Val-2
Val-3|Val-4", sep = "|")
df
# Column_A Column_B
#1 Val-1 Val-2
#2 Val-3 Val-4
Upvotes: 0
Reputation: 2330
You're looking for read.table
:
df <- read.table(text="
Column_A|Column_B
Val-1|Val-2
Val-3|Val-4", header=TRUE, sep="|")
Upvotes: 5