[R] how to reach a txt file like this?

Boris Steipe boris.steipe at utoronto.ca
Tue Jun 9 19:20:58 CEST 2015


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.



More information about the R-help mailing list