我一直在尝试学习purrr,因为我一直在处理一些深层嵌套的JSON数据,但是我不断收到似乎不在在线其他地方出现的错误。以下是我的JSON数据/代码:
json <- {"countries":[[{"holdings":[{"quantity":"50,000","cost":"7,597,399","currency":"USD","evaluation":"6,853,500","percentageOfNetAssets":"4.52","title":"Alibaba Group Holding Ltd ADR ","page":"66"},{"quantity":"625,000","cost":"1,842,933","currency":"HKD","evaluation":"3,033,457","percentageOfNetAssets":"2.00","title":"Anhui Conch Cement Co Ltd ","page":"66"},{"quantity":"1,949,000","cost":"1,480,711","currency":"HKD","evaluation":"0","percentageOfNetAssets":"0.00","title":"China Animal Healthcare Ltd ","page":"66"},{"quantity":"2,888,000","cost":"2,992,011","currency":"HKD","evaluation":"2,382,890","percentageOfNetAssets":"1.57","title":"China Construction Bank Corp ","page":"66"},{"quantity":"2,030,000","cost":"2,994,592","currency":"HKD","evaluation":"3,137,298","percentageOfNetAssets":"2.07","title":"CNOOC Ltd ","page":"66"},{"quantity":"400,000","cost":"3,127,007","currency":"HKD","evaluation":"3,548,187","percentageOfNetAssets":"2.34","title":"ENN Energy Holdings Ltd ","page":"66"},{"quantity":"349,297","cost":"3,288,042","currency":"CNH","evaluation":"3,497,876","percentageOfNetAssets":"2.31","title":"Foshan Haitian Flavouring & Food Co Ltd ","page":"66"},{"quantity":"929,012","cost":"3,187,360","currency":"CNH","evaluation":"3,093,845","percentageOfNetAssets":"2.04","title":"Inner Mongolia Yili Industrial Group Co Ltd ","page":"66"},{"quantity":"630,000","cost":"4,720,889","currency":"HKD","evaluation":"5,564,255","percentageOfNetAssets":"3.67","title":"Ping An Insurance Group Co of China Ltd ","page":"66"},{"quantity":"422,000","cost":"3,030,250","currency":"HKD","evaluation":"4,783,603","percentageOfNetAssets":"3.16","title":"Shenzhou International Group Holdings Ltd ","page":"66"},{"quantity":"250,000","cost":"7,161,493","currency":"HKD","evaluation":"10,026,375","percentageOfNetAssets":"6.61","title":"Tencent Holdings Ltd ","page":"66"},{"quantity":"986,000","cost":"2,263,521","currency":"HKD","evaluation":"2,525,024","percentageOfNetAssets":"1.67","title":"TravelSky Technology Ltd ","page":"66"},{"quantity":"50,600","cost":"5,969,458","currency":"USD","evaluation":"2,956,558","percentageOfNetAssets":"1.95","title":"Weibo Corp ADR ","page":"66"},{"quantity":"100,000","cost":"3,581,365","currency":"USD","evaluation":"3,353,000","percentageOfNetAssets":"2.21","title":"Yum China Holdings Inc ","page":"66"}],"total":{"page":"66","cost":"53,237,031","evaluation":"54,755,868","percentageOfNetAssets":"36.12"},"title":"China ","page":"66"},{"holdings":[{"quantity":"1,130,000","cost":"8,812,716","currency":"HKD","evaluation":"9,381,366","percentageOfNetAssets":"6.19","title":"AIA Group Ltd ","page":"66"},{"quantity":"700,000","cost":"3,099,505","currency":"HKD","evaluation":"2,601,749","percentageOfNetAssets":"1.71","title":"BOC Hong Kong Holdings Ltd ","page":"66"},{"quantity":"12,500,000","cost":"2,685,700","currency":"HKD","evaluation":"2,378,869","percentageOfNetAssets":"1.57","title":"Pacific Basin Shipping Ltd ","page":"66"}],"total":{"page":"66","cost":"14,597,921","evaluation":"14,361,984","percentageOfNetAssets":"9.47"},"title":"Hong Kong ","page":"66"},{"holdings":[{"quantity":"380,000","cost":"2,713,957","currency":"INR","evaluation":"2,731,820","percentageOfNetAssets":"1.80","title":"Future Retail Ltd ","page":"66"},{"quantity":"135,000","cost":"3,260,344","currency":"INR","evaluation":"3,806,163","percentageOfNetAssets":"2.51","title":"Housing Development Finance Corp Ltd ","page":"66"},{"quantity":"192,556","cost":"2,280,854","currency":"INR","evaluation":"4,411,012","percentageOfNetAssets":"2.91","title":"IndusInd Bank Ltd ","page":"66"},{"quantity":"40,000","cost":"2,584,823","currency":"INR","evaluation":"4,277,304","percentageOfNetAssets":"2.82","title":"Maruti Suzuki India Ltd ","page":"66"},{"quantity":"89,000","cost":"2,460,333","currency":"INR","evaluation":"2,413,256","percentageOfNetAssets":"1.59","title":"Tata Consultancy Services Ltd ","page":"66"},{"quantity":"265,281","cost":"3,375,523","currency":"INR","evaluation":"3,537,586","percentageOfNetAssets":"2.34","title":"Titan Co Ltd ","page":"66"}],"total":{"page":"66","cost":"16,675,834","evaluation":"21,177,141","percentageOfNetAssets":"13.97"},"title":"India ","page":"66"},{"holdings":[{"quantity":"3,280,100","cost":"4,020,564","currency":"IDR","evaluation":"5,930,640","percentageOfNetAssets":"3.91","title":"Bank Central Asia Tbk PT ","page":"66"}],"total":{"page":"66","cost":"4,020,564","evaluation":"5,930,640","percentageOfNetAssets":"3.91"},"title":"Indonesia ","page":"66"},{"holdings":[{"quantity":"1,550,000","cost":"3,239,787","currency":"MYR","evaluation":"3,143,134","percentageOfNetAssets":"2.07","title":"Malaysia Airports Holdings Bhd ","page":"66"}],"total":{"page":"66","cost":"3,239,787","evaluation":"3,143,134","percentageOfNetAssets":"2.07"},"title":"Malaysia ","page":"66"},{"holdings":[{"quantity":"3,687,000","cost":"2,747,584","currency":"PHP","evaluation":"2,846,671","percentageOfNetAssets":"1.88","title":"Ayala Land Inc ","page":"66"}],"total":{"page":"66","cost":"2,747,584","evaluation":"2,846,671","percentageOfNetAssets":"1.88"},"title":"Philippines ","page":"66"},{"holdings":[{"quantity":"234,969","cost":"4,056,529","currency":"SGD","evaluation":"4,083,944","percentageOfNetAssets":"2.69","title":"DBS Group Holdings Ltd ","page":"66"}],"total":{"page":"66","cost":"4,056,529","evaluation":"4,083,944","percentageOfNetAssets":"2.69"},"title":"Singapore ","page":"66"},{"holdings":[{"quantity":"18,000","cost":"6,727,232","currency":"KRW","evaluation":"5,597,777","percentageOfNetAssets":"3.69","title":"LG Chem Ltd ","page":"66"},{"quantity":"3,800","cost":"4,844,479","currency":"KRW","evaluation":"3,749,597","percentageOfNetAssets":"2.48","title":"LG Household & Health Care Ltd ","page":"66"},{"quantity":"171,000","cost":"5,875,373","currency":"KRW","evaluation":"5,930,902","percentageOfNetAssets":"3.91","title":"Samsung Electronics Co Ltd ","page":"66"},{"quantity":"40,000","cost":"2,595,521","currency":"KRW","evaluation":"2,168,847","percentageOfNetAssets":"1.43","title":"SK Hynix Inc ","page":"66"}],"total":{"page":"66","cost":"20,042,605","evaluation":"17,447,123","percentageOfNetAssets":"11.51"},"title":"South Korea ","page":"66"},{"holdings":[{"quantity":"244,000","cost":"2,788,471","currency":"TWD","evaluation":"1,786,121","percentageOfNetAssets":"1.18","title":"Catcher Technology Co Ltd ","page":"67"},{"quantity":"23,000","cost":"3,167,793","currency":"TWD","evaluation":"2,405,732","percentageOfNetAssets":"1.59","title":"Largan Precision Co Ltd ","page":"67"},{"quantity":"1,400,000","cost":"8,639,650","currency":"TWD","evaluation":"10,271,009","percentageOfNetAssets":"6.77","title":"Taiwan Semiconductor Manufacturing Co Ltd ","page":"67"}],"total":{"page":"67","cost":"14,595,914","evaluation":"14,462,862","percentageOfNetAssets":"9.54"},"title":"Taiwan ","page":"67"},{"holdings":[{"quantity":"1,739,000","cost":"3,416,652","currency":"THB","evaluation":"3,431,534","percentageOfNetAssets":"2.27","title":"Airports of Thailand PCL ","page":"67"},{"quantity":"1,269,500","cost":"2,379,289","currency":"THB","evaluation":"2,914,470","percentageOfNetAssets":"1.92","title":"Central Pattana PCL ","page":"67"}],"total":{"page":"67","cost":"Total - Transferable securities admitted to an official stock exchange listing 139,009,710","evaluation":"144,555,371","percentageOfNetAssets":"95.35"},"title":"Thailand ","page":"67"}]],"total":[{}],"page":["66"],"title":["Shares "]}
library(jsonlite)
library(purrr)
library(magrittr)
test <- fromJSON(json) %>% purrr::map(extract, c("page", "title"))
Error in `[.data.frame`(.x, ...) : undefined columns selected
基本上,我试图查看is.unsorted(test$countries[[1]]$title)
是否返回true,如果是,那么我需要保留test$page
。我以为提取国家和页面是最好的方法,但是我对purrr非常陌生,并且在网上看到的提取(或[
)方法不起作用。有人看到我在做什么错,还是知道其他方法?
答案 0 :(得分:1)
如果您想返回列表中的某个元素(如果其他条件正确的话),为什么不只是简单的ifelse?
data <- fromJSON(json)
ifelse(is.unsorted(data$countries[[1]]$title), data$page, FALSE)
# Look at negation (so if it's not unsorted)
ifelse(!is.unsorted(data$countries[[1]]$title), data$page, FALSE)