Sonal
Sonal

Reputation: 581

Convert (csv) string object to data frame

My character object looks like this:

"ID,MONTH_ID,FLAG\n70,201001,1\n71,201001,1\n94,201001,1\n95,201001,1\n102,201001,
1\n110,201001,1\n124,201001,1"

I want to convert it to data frame with every \n a new row should be created.

Can someone please help me in this?

Upvotes: 5

Views: 6475

Answers (2)

Aramis7d
Aramis7d

Reputation: 2496

try :

x <- "ID,MONTH_ID,FLAG\n70,201001,1\n71,201001,1\n94,201001,1\n95,201001,1\n102,201001,1\n110,201001,1\n124,201001,1"
df <- read.table(text = x, sep =",", header = TRUE, stringsAsFactors = FALSE)

which gives:

   ID MONTH_ID FLAG
1  70   201001    1
2  71   201001    1
3  94   201001    1
4  95   201001    1
5 102   201001    1
6 110   201001    1
7 124   201001    1

Upvotes: 10

Adamm
Adamm

Reputation: 2306

x <- "ID,MONTH_ID,FLAG\n70,201001,1\n71,201001,1\n94,201001,1\n95,201001,1\n102,201001,1\n110,201001,1\n124,201001,1"
l <- strsplit(x, "\n")
df <- as.data.frame(l)

Upvotes: 0

Related Questions