?Hey All, I have a txt data file that looks like this: ?[{?ID???A???Name":"Tom", "Age":"18"},{?ID???B???Name":"Jim", "Age":"19"}] ?How can I read this into R as a data frame? I have used readLines to read all the lines but dont know how to deal with column names and inputs. Thanks for your help!? [[alternative HTML version deleted]]
On Tue, Jun 9, 2015 at 11:24 AM, Ye Lin <yelin at lbl.gov> wrote:> ?Hey All, I have a txt data file that looks like this: > > ?[{?ID???A???Name":"Tom", "Age":"18"},{?ID???B???Name":"Jim", "Age":"19"}] > > > ?How can I read this into R as a data frame? I have used readLines to read > all the lines but dont know how to deal with column names and inputs. >?That looks like a JSON array of objects to me. I would look into "jsonlite", "rjson", or "RJSONIO" on CRAN. You'll need to review them to see which best meets your needs.?> > Thanks for your help!? > > [[alternative HTML version deleted]] >? Please change to plain text. In many cases HTML displays poorly due to the list trying to change it for you to plain text. And, in that case, you'll likely be ignored. ? -- Yoda of Borg, we are. Futile, resistance is, yes. Assimilated, you will be. My sister opened a computer store in Hawaii. She sells C shells down by the seashore. If someone tell you that nothing is impossible: Ask him to dribble a football. He's about as useful as a wax frying pan. 10 to the 12th power microphones = 1 Megaphone Maranatha! <>< John McKown [[alternative HTML version deleted]]
This is (almost) json data (but see NOTE below); there are several packages that deal with json, jsonlite for example. R > data <- '[{"ID":"A", "Name":"Tom", "Age":"18"},{"ID":"B", "Name":"Jim", "Age":"19"}]' R > install.packages("jsonlite") R > library(jsonlite) R > myDf <- fromJSON(data, simplifyDataFrame=TRUE) R > str(myDf) 'data.frame': 2 obs. of 3 variables: $ ID : chr "A" "B" $ Name: chr "Tom" "Jim" $ Age : chr "18" "19" NOTE: some of the quotation marks in your example are messed up, and some of your commas and colons seem to use an Asian font - i.e. they are UTF, not ASCII. You will need to clean up all the non ASCII characters that are syntactically important, otherwise things break. Cheers, Boris On Jun 9, 2015, at 12:24 PM, Ye Lin <yelin at lbl.gov> wrote:> ?Hey All, I have a txt data file that looks like this: > > ?[{?ID???A???Name":"Tom", "Age":"18"},{?ID???B???Name":"Jim", "Age":"19"}] > > > ?How can I read this into R as a data frame? I have used readLines to read > all the lines but dont know how to deal with column names and inputs. > > Thanks for your help!? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.