How to read csv but separating only the first two separators with a comma?

I have a CSV file. I want to read the file in R, but use only the first 2 commas, i.e. If there is such a line in the file,

1,1000, I finished with you

In R, I want it to be a data frame row with three columns like this

> df <- data.frame("Id"="1","Count" ="1000", "Comment" = "I, am done, with you")
> df
  Id Count              Comment
1  1  1000 I, am done, with you
+3
source share
1 answer

A regular expression will work.

For example, suppose strthese are the lines that you want to recognize. Suppose your csv file looks like

1,1000,I, am done, with you
2,500, i don't know

If you want to read from a file, just call readLines()to read all the lines of the file as a character vector in R, just like str.

. {stringr}, .

str <- c("1,1000,I, am done, with you", "2,500, i don't know")

library(stringr)

# match the strings by pattern integer,integer,anything
matches <- str_match(str,pattern="(\\d+),(\\d+),\\s*(.+)")

(\\d+),(\\d+),\\s*(.+). \\d , \\s , . -. + , * "" "-". () , , .

matches,

     [,1]                          [,2] [,3]   [,4]                  
[1,] "1,1000,I, am done, with you" "1"  "1000" "I, am done, with you"
[2,] "2,500, i don't know"         "2"  "500"  "i don't know"        

, str_match . , .

df <- data.frame(matches[,-1],stringsAsFactors=F)
colnames(df) <- c("Id","Count","Comment")
df <- transform(df,Id=as.integer(Id),Count=as.integer(Count))

df - :

  Id Count              Comment
1  1  1000 I, am done, with you
2  2  1002         i don't know
+4

All Articles