r - How to read a csv but separating only at first two comma separators? -


I have a CSV file I want to read the file in R, but only use the first 2 commas, i.e. There is one such row in the file,

1,1000, I did it with you

Wants to do with three pillars

  & gt; Df & lt; - data.frame ("id" = "1", "calculation" = "1000", "comment" = "I, done with you") & gt; DF ID comment 1 1 1000 I, with    

regular expressions will be working.

For example, it seems that there are str lines that you want to recognize here that your CSV file appears

  1, 1000, I, with you 2,500, I do not know   

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

The technique is very simple I use the {stringr} package to match the text and the information I need is used to remove it.

  str & lt; - c ("1,1000, I have done, with you", "2,500, I do not know") Library (stringer) # String pattern integer, integer, few matches and lieutenant; - Combine str_match (str, pattern = "(\\ d +), (\\), D +), \\ s * (. +)")   

Here I summarize Pattern (\\ d +), (\\ d +), \\ s * (. +) . Represents the \\ d number; \\ s displays the location character, . represents anything + means one or more, * none or nothing () grouping patterns So that the function knows what we believe as a group of information.

If you look at matches , then it seems that "1" "1000" "I, has been done with you" "1," [1,]

look, str_match The / Code> function successfully divides the text into a pattern from a matrix, so our task is to replace the matrix with the correct data type with only the data form.

  df <- data .frame (matches [, - 1], stringsfactor = f) colnames (df) (df, id = as.integer Ed), calculation = as.integer (calculation))      pre> 

df Our goal is:

  ID count comment 1 1 1000 i, 2 2 1002 with you I do not know    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -