5

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?

2 Answers 2

10

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
Sign up to request clarification or add additional context in comments.

1 Comment

@Sonal glad to know. you may check here
-1
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.