Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert (csv) string object to data frame

Tags:

dataframe

r

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?

like image 622
Sonal Avatar asked Feb 04 '23 14:02

Sonal


1 Answers

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
like image 90
Aramis7d Avatar answered Feb 07 '23 09:02

Aramis7d