0

I'm pretty new to R. I have done some search but still have trouble with this issue.

This is an example of my data (CSV) out of 100,000 records

{id_outlet_delivery:22015,name:Branch A}6 ,

{id_outlet_delivery:22016,name:Branch B}7 ,

{id_outlet_delivery:22017,name:Branch C}8

What I want is to convert this to data frame so that I can perform some analysis. (I don't want the number 6,7,8 as in the example to be in the data frame).

2
  • I guess this could be done using packages like RJSONIO or jsonlite. Commented Oct 19, 2014 at 4:24
  • Any of the three most popular JSON drivers (packages) in R can convert a JSON string to R objects. That's not a proper CSV file since the JSON strings aren't quoted in it (and, I can't think of a CSV file library that will read that by default). Your best bet is to use readLines and either convert the whole thing to a proper JSON string/format or parse each line separately and combine everything into data frame rows. Commented Oct 19, 2014 at 4:29

1 Answer 1

1

Here is a method using regex

 library(stringr)
  v1 <- '{id_outlet_delivery:22015,name:Branch A}6 ,
    {id_outlet_delivery:22016,name:Branch B}7 ,
    {id_outlet_delivery:22017,name:Branch C}8'

  nm1 <- str_extract_all(v1, perl("[[:alpha:]_]+(?=:)"))[[1]][1:2]
  val <- str_extract_all(v1, perl("(?<=:)[[:alnum:] ]+(?=\\})?"))[[1]]

Or you could use stringi which would be faster

  library(stringi)
  nm1 <- stri_extract_all_regex(v1, "[[:alpha:]_]+(?=:)")[[1]][1:2]
  val <- stri_extract_all_regex(v1, "(?<=:)[[:alnum:] ]+(?=\\}|,)")[[1]]


  indx <- c(TRUE, FALSE)
  dat <- setNames(data.frame(as.numeric(val[indx]), val[!indx],
                                     stringsAsFactors=FALSE), nm1)

  dat
  #  id_outlet_delivery     name
  #1              22015 Branch A
  #2              22016 Branch B
  #3              22017 Branch C
Sign up to request clarification or add additional context in comments.

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.