如何阅读R

时间:2017-06-02 07:39:10

标签: json r csv dataframe extract

我有一个包含2列的csv文件,即ID,JSON_OBJECT

ID  JSON_OBJECT
1   {"results" :[ {  "address_components"[{"long_name" : "Unnamed Road",     
    "short_name" :"Unnamed Road"],"formatted_address":"Unnamed Road,Punjab 
    160003, India","geometry" : {"bounds" : {"northeast" : {"lat" : 
    30.6593213,"lng" :76.7952105},"southwest" : {"lat" : 30.6548837,"lng" 
    76.7931289}},"location" :{"lat" : 30.6576094,"lng" : 
    76.794392}],"formatted_address" : "160004, India",          "geometry" : 
    {"bounds" : {"northeast" : {"lat" : 30.6832516,"lng" : 76.8121771                
    },"southwest" : {"lat" : 30.6447985,"lng" : 76.7639648}},"location" : {"lat" 
    :30.6582706,"lng" :76.7855866}

2   {"results" :[ {  "address_components"[{"long_name" : "hertz Road",     
    "short_name" :"Karl Road"],"formatted_address":"hertz Road,Chennai
    600003, India","geometry" : {"bounds" : {"Southwest" : {"lat" : 
    27.7693211,"lng" :12.4052104},"southwest" : {"lat" : 70.2348881,"lng" 
    98.2331289}},"location" :{"lat" : 28.9176092,"lng" : 
    13.544391}],"formatted_address" : "600002, India", "geometry" : 
    {"bounds" : {"northeast" : {"lat" : 20.3832518,"lng" : 61.2121776                
    },"southwest" : {"lat" : 77.5447986,"lng" : 73.5639645}},"location" : {"lat" 
    :40.1282709,"lng" :12.7855862}
3   ......
4   ......
5   ......

我想从第二列(json_OBJECT)中提取少量组件或标签,我们可以看到标签 formatted_address 每行有两次或多次,我想要访问第一次出现的formatted_address内容, lat lng 位于标签位置下,并将这三个内容存储为单独的数据帧

formatted_add                               lat              lng     
Unnamed Road,Punjab,160003, India         30.6576094      76.794392
hertz Road,Chennai,600003, India          28.9176092      13.544391
....                                         ...             ...
....                                         ...             ...

这就是我试过的

df=read.csv("D:/Deepesh/All files/few_json_content.csv",stringsAsFactors = F)
df.list <- lapply(df$JSON_OBJECT, function(x) fromJSON(x))

但是这并没有给出我期望的正确数据,我们非常感谢任何帮助,谢谢

来自实际csv的几行(仅发布第二列数据)

           JSON_OBJECT
{    "results" : [       {          "address_components" : [             {                "long_name" : "Unnamed Road",                "short_name" : "Unnamed Road",                "types" : [ "route" ]             },             {                "long_name" : "Bhabat",                "short_name" : "Bhabat",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             },             {                "long_name" : "160003",                "short_name" : "160003",                "types" : [ "postal_code" ]             }          ],          "formatted_address" : "Unnamed Road, Bhabat, Punjab 160003, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.6589192,                   "lng" : 76.80246939999999                },                "southwest" : {                   "lat" : 30.6588485,                   "lng" : 76.80187579999999                }             },             "location" : {                "lat" : 30.65891659999999,                "lng" : 76.8021791             },             "location_type" : "GEOMETRIC_CENTER",             "viewport" : {                "northeast" : {                   "lat" : 30.66023283029149,                   "lng" : 76.80352158029149                },                "southwest" : {                   "lat" : 30.6575348697085,                   "lng" : 76.80082361970848                }             }          },          "place_id" : "ChIJjXj4DWrrDzkR6iBhePDBGMA",          "types" : [ "route" ]       },       {          "address_components" : [             {                "long_name" : "Bhabat",                "short_name" : "Bhabat",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Bhabat, Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.67249,                   "lng" : 76.81895999999999                },                "southwest" : {                   "lat" : 30.64508,                   "lng" : 76.79106999999999                }             },             "location" : {                "lat" : 30.6575248,                "lng" : 76.8057776             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.67249,                   "lng" : 76.81895999999999                },                "southwest" : {                   "lat" : 30.64508,                   "lng" : 76.79106999999999                }             }          },          "place_id" : "ChIJN3sGJEHrDzkRwGZ1GMXBa2Q",          "types" : [ "locality", "political" ]       },       {          "address_components" : [             {                "long_name" : "Chandigarh Airport Area",                "short_name" : "Chandigarh Airport Area",                "types" : [ "political", "sublocality", "sublocality_level_1" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "Chandigarh",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "CH",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Chandigarh Airport Area, Chandigarh, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.696639,                   "lng" : 76.81389089999999                },                "southwest" : {                   "lat" : 30.6568821,                   "lng" : 76.76542999999999                }             },             "location" : {                "lat" : 30.6783321,                "lng" : 76.7884713             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.696639,                   "lng" : 76.81389089999999                },                "southwest" : {                   "lat" : 30.6568821,                   "lng" : 76.76542999999999                }             }          },          "place_id" : "ChIJcb4-lILsDzkRJIQJTpsMm8w",          "types" : [ "political", "sublocality", "sublocality_level_1" ]       },       {          "address_components" : [             {                "long_name" : "160004",                "short_name" : "160004",                "types" : [ "postal_code" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "160004, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.6832516,                   "lng" : 76.8121771                },                "southwest" : {                   "lat" : 30.6447985,                   "lng" : 76.7639648                }             },             "location" : {                "lat" : 30.6582706,                "lng" : 76.7855866             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.6832516,                   "lng" : 76.8121771                },                "southwest" : {                   "lat" : 30.6447985,                   "lng" : 76.7639648                }             }          },          "place_id" : "ChIJf560w6fsDzkRXLKn0s52kHM",          "types" : [ "postal_code" ]       },       {          "address_components" : [             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Sahibzada Ajit Singh Nagar, Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.9369199,                   "lng" : 76.9439199                },                "southwest" : {                   "lat" : 30.35902,                   "lng" : 76.52444009999999                }             },             "location" : {                "lat" : 30.6496486,                "lng" : 76.7567368             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.9369199,                   "lng" : 76.9439199                },                "southwest" : {                   "lat" : 30.35902,                   "lng" : 76.52444009999999                }             }          },          "place_id" : "ChIJOyhuuFPlDzkRzrRj0bvdZPw",          "types" : [ "administrative_area_level_2", "political" ]       },       {          "address_components" : [             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 32.4994008,                   "lng" : 76.92175809999999                },                "southwest" : {                   "lat" : 29.537147,                   "lng" : 73.88057999999999                }             },             "location" : {                "lat" : 31.1471305,                "lng" : 75.34121789999999             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 32.4994008,                   "lng" : 76.92175809999999                },                "southwest" : {                   "lat" : 29.537147,                   "lng" : 73.88057999999999                }             }          },          "place_id" : "ChIJVXOeVqpkGTkRYYijAzEmvY8",          "types" : [ "administrative_area_level_1", "political" ]       },       {          "address_components" : [             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 35.5087008,                   "lng" : 97.39535869999999                },                "southwest" : {                   "lat" : 6.4626999,                   "lng" : 68.1097                }             },             "location" : {                "lat" : 20.593684,                "lng" : 78.96288             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 35.5087008,                   "lng" : 97.39498069999999                },                "southwest" : {                   "lat" : 6.7535159,                   "lng" : 68.16288519999999                }             }          },          "place_id" : "ChIJkbeSa_BfYzARphNChaFPjNc",          "types" : [ "country", "political" ]       }    ],    "status" : "OK" }

row(2)
{    "results" : [       {          "address_components" : [             {                "long_name" : "Unnamed Road",                "short_name" : "Unnamed Road",                "types" : [ "route" ]             },             {                "long_name" : "Bhabat",                "short_name" : "Bhabat",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             },             {                "long_name" : "160003",                "short_name" : "160003",                "types" : [ "postal_code" ]             }          ],          "formatted_address" : "Unnamed Road, Bhabat, Punjab 160003, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.6592914,                   "lng" : 76.80716269999999                },                "southwest" : {                   "lat" : 30.6581647,                   "lng" : 76.80569129999999                }             },             "location" : {                "lat" : 30.6590514,                "lng" : 76.80666819999999             },             "location_type" : "GEOMETRIC_CENTER",             "viewport" : {                "northeast" : {                   "lat" : 30.6600770302915,                   "lng" : 76.80777598029151                },                "southwest" : {                   "lat" : 30.6573790697085,                   "lng" : 76.80507801970849                }             }          },          "place_id" : "ChIJE3brDUHrDzkR0JUm6CO-K-U",          "types" : [ "route" ]       },       {          "address_components" : [             {                "long_name" : "Bhabat",                "short_name" : "Bhabat",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Bhabat, Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.67249,                   "lng" : 76.81895999999999                },                "southwest" : {                   "lat" : 30.64508,                   "lng" : 76.79106999999999                }             },             "location" : {                "lat" : 30.6575248,                "lng" : 76.8057776             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.67249,                   "lng" : 76.81895999999999                },                "southwest" : {                   "lat" : 30.64508,                   "lng" : 76.79106999999999                }             }          },          "place_id" : "ChIJN3sGJEHrDzkRwGZ1GMXBa2Q",          "types" : [ "locality", "political" ]       },       {          "address_components" : [             {                "long_name" : "Chandigarh Airport Area",                "short_name" : "Chandigarh Airport Area",                "types" : [ "political", "sublocality", "sublocality_level_1" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "Chandigarh",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "CH",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Chandigarh Airport Area, Chandigarh, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.696639,                   "lng" : 76.81389089999999                },                "southwest" : {                   "lat" : 30.6568821,                   "lng" : 76.76542999999999                }             },             "location" : {                "lat" : 30.6783321,                "lng" : 76.7884713             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.696639,                   "lng" : 76.81389089999999                },                "southwest" : {                   "lat" : 30.6568821,                   "lng" : 76.76542999999999                }             }          },          "place_id" : "ChIJcb4-lILsDzkRJIQJTpsMm8w",          "types" : [ "political", "sublocality", "sublocality_level_1" ]       },       {          "address_components" : [             {                "long_name" : "160004",                "short_name" : "160004",                "types" : [ "postal_code" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "160004, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.6832516,                   "lng" : 76.8121771                },                "southwest" : {                   "lat" : 30.6447985,                   "lng" : 76.7639648                }             },             "location" : {                "lat" : 30.6582706,                "lng" : 76.7855866             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.6832516,                   "lng" : 76.8121771                },                "southwest" : {                   "lat" : 30.6447985,                   "lng" : 76.7639648                }             }          },          "place_id" : "ChIJf560w6fsDzkRXLKn0s52kHM",          "types" : [ "postal_code" ]       },       {          "address_components" : [             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Sahibzada Ajit Singh Nagar, Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.9369199,                   "lng" : 76.9439199                },                "southwest" : {                   "lat" : 30.35902,                   "lng" : 76.52444009999999                }             },             "location" : {                "lat" : 30.6496486,                "lng" : 76.7567368             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.9369199,                   "lng" : 76.9439199                },                "southwest" : {                   "lat" : 30.35902,                   "lng" : 76.52444009999999                }             }          },          "place_id" : "ChIJOyhuuFPlDzkRzrRj0bvdZPw",          "types" : [ "administrative_area_level_2", "political" ]       },       {          "address_components" : [             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 32.4994008,                   "lng" : 76.92175809999999                },                "southwest" : {                   "lat" : 29.537147,                   "lng" : 73.88057999999999                }             },             "location" : {                "lat" : 31.1471305,                "lng" : 75.34121789999999             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 32.4994008,                   "lng" : 76.92175809999999                },                "southwest" : {                   "lat" : 29.537147,                   "lng" : 73.88057999999999                }             }          },          "place_id" : "ChIJVXOeVqpkGTkRYYijAzEmvY8",          "types" : [ "administrative_area_level_1", "political" ]       },       {          "address_components" : [             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 35.5087008,                   "lng" : 97.39535869999999                },                "southwest" : {                   "lat" : 6.4626999,                   "lng" : 68.1097                }             },             "location" : {                "lat" : 20.593684,                "lng" : 78.96288             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 35.5087008,                   "lng" : 97.39498069999999                },                "southwest" : {                   "lat" : 6.7535159,                   "lng" : 68.16288519999999                }             }          },          "place_id" : "ChIJkbeSa_BfYzARphNChaFPjNc",          "types" : [ "country", "political" ]       }    ],    "status" : "OK" }

0 个答案:

没有答案
相关问题