2017-10-19 61 views
0

我已经广泛地尝试回答这个问题,但是我似乎无法将任何现有的解决方案适用于我的案例(或者我无法理解如何这样做)。 林尝试使用下面的代码(工作)从R提取特定数据时出错

##location details (works) 
require(httr) 

URL <- 'https://developers.zomato.com/api/v2.1/search?' 

request <- GET(URL, 
      add_headers(User_key=""), 
      query=list(entity_id = '260', 
         entity_type = 'city')) 
content(request) 
ZomatoData <-content(request) 

然后我得到以下响应提取API调用数据:

$restaurants[[13]] 
$restaurants[[13]]$restaurant 
$restaurants[[13]]$restaurant$R 
$restaurants[[13]]$restaurant$R$res_id 
[1] 16562670 


$restaurants[[13]]$restaurant$apikey 
[1] "" 

$restaurants[[13]]$restaurant$id 
[1] "16562670" 

$restaurants[[13]]$restaurant$name 
[1] "Home Thai" 

$restaurants[[13]]$restaurant$url 
[1] "https://www.zomato.com/sydney/home-thai-cbd? 
utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1" 

$restaurants[[13]]$restaurant$location 
$restaurants[[13]]$restaurant$location$address 
[1] "Shop 1-2, 299 Sussex Street, CBD, Sydney" 

$restaurants[[13]]$restaurant$location$locality 
[1] "CBD" 

$restaurants[[13]]$restaurant$location$city 
[1] "Sydney" 

$restaurants[[13]]$restaurant$location$city_id 
[1] 260 

$restaurants[[13]]$restaurant$location$latitude 
[1] "-33.8744859237" 

$restaurants[[13]]$restaurant$location$longitude 
[1] "151.2044165656" 

$restaurants[[13]]$restaurant$location$zipcode 
[1] "2000" 

$restaurants[[13]]$restaurant$location$country_id 
[1] 14 

$restaurants[[13]]$restaurant$location$locality_verbose 
[1] "CBD, Sydney" 


$restaurants[[13]]$restaurant$switch_to_order_menu 
[1] 0 

$restaurants[[13]]$restaurant$cuisines 
[1] "Thai, Salad" 

$restaurants[[13]]$restaurant$average_cost_for_two 
[1] 60 

$restaurants[[13]]$restaurant$price_range 
[1] 3 

$restaurants[[13]]$restaurant$currency 
[1] "$" 

$restaurants[[13]]$restaurant$offers 
list() 

$restaurants[[13]]$restaurant$user_rating 
$restaurants[[13]]$restaurant$user_rating$aggregate_rating 
[1] "4.5" 

这是一切优秀,但是我想只提取用户评分汇总评级值,并将其写入CSV文件,使其具有2列,其中1个是餐厅名称,另一个是评级。想知道是否有人可以帮忙吗? 任何善良和乐于助人的反应是极大的赞赏 更新:这里是输出STR(ZomatoData)

> str(ZomatoData) 
List of 4 
$ results_found: int 16056 
$ results_start: int 0 
$ results_shown: int 20 
$ restaurants :List of 20 
..$ :List of 1 
.. ..$ restaurant:List of 23 
.. .. ..$ R     :List of 1 
.. .. .. ..$ res_id: int 16564875 
.. .. ..$ apikey    : chr "api-key" 
.. .. ..$ id     : chr "16564875" 
.. .. ..$ name    : chr "The Grounds of Alexandria Cafe" 
.. .. ..$ url     : chr "https://www.zomato.com/sydney/the- 
grounds-of-alexandria-cafe-alexandria? 
utm_source=api_basic_user&utm_medium=ap"| __truncated__ 
.. .. ..$ location   :List of 9 
.. .. .. ..$ address   : chr "Shop 7A, 2 Huntley Street, Alexandria, 
Sydney" 
.. .. .. ..$ locality  : chr "The Grounds of Alexandria, Alexandria" 
.. .. .. ..$ city   : chr "Sydney" 
.. .. .. ..$ city_id   : int 260 
.. .. .. ..$ latitude  : chr "-33.9110760390" 
.. .. .. ..$ longitude  : chr "151.1936605722" 
.. .. .. ..$ zipcode   : chr "2015" 
.. .. .. ..$ country_id  : int 14 
.. .. .. ..$ locality_verbose: chr "The Grounds of Alexandria, Alexandria, 
Sydney" 
.. .. ..$ switch_to_order_menu: int 0 
.. .. ..$ cuisines   : chr "Cafe, Coffee and Tea, Salad" 
.. .. ..$ average_cost_for_two: int 80 
.. .. ..$ price_range   : int 3 
.. .. ..$ currency   : chr "$" 
.. .. ..$ offers    : list() 
.. .. ..$ thumb    : chr 

.. .. ..$ user_rating   :List of 4 
.. .. .. ..$ aggregate_rating: chr "4.6" 
.. .. .. ..$ rating_text  : chr "Excellent" 
.. .. .. ..$ rating_color : chr "3F7E00" 
.. .. .. ..$ votes   : chr "3162" 
+0

更新,我已经尝试过的事情像write.csv,它给我的错误: > write.csv(ZomatoData, “C:/Users/sanaz/Desktop/Zomato.csv”) 错误(函数( ...,row.names = NULL,check.rows = FALSE,check.names = TRUE,: 参数意味着不同的行数:1,0 这是可以理解的,因为我试图写入的内容不适合矩形电子表格。这就是为什么我卡住了。 –

+0

当你做'长度(ZomatoData $餐馆)时,你会得到什么' –

+0

@Hardikgupta我得到了'[1] 20' –

回答

0

我无法重现上面的示例代码,你不给一个小例子,数据集,所以我没有办法对您的数据进行测试。

这且不说,你可以尝试以下方法:

df <- do.call(rbind.data.frame, 
    lapply(ZomatoData$restaurants, function(x) { 
     c(x$restaurant$name, x$restaurant$user_rating$aggregate_rating) })); 
write.csv(df, file = "summary.csv"); 

这里restaurants应该与你给结构的列表。

+0

嗨@MauritsEvers,我试过你的建议,但是我得到这个错误: do.call错误(rbind.data,frame,lapply(restaurants,function(x){: 第二个参数必须是一个列表 我正在尝试错误? –

+0

正如我在我的文章中所说的,不幸的是,你不会给出一个最小的数据集。你能否更新你的问题来包含'str(ZomatoData)'的输出呢?或者更好的是,包含一个最小数据集 –

+0

抱歉你再一次@Maurits,如你所请求的那样放入str(ZomatoData)的输出,是的,我很抱歉,由于Api Key的限制,代码是不可复制的,如果没有其他解决方案,我会玩周围有你所建议的编码,并希望它会给我一些东西。再次感谢! –