I was trying to subset a large list with 278226 elements and each element (shown as below) is also a list which has a number(between 39 and 50) of sub-elements(size 1 atomic vector with different names).
> str(listings_England[9922])
List of 1
$ listing:List of 40
..$ agent_address : chr "35 John Street, Luton"
..$ agent_logo : chr "https://st.zoocdn.com/zoopla_static_agent_logo_(257607).png"
..$ agent_name : chr "Ashton Carter Homes"
..$ agent_phone : chr "020 8115 4543"
..$ category : chr "Residential"
..$ country : NULL
..$ country_code : chr "gb"
..$ county : NULL
..$ displayable_address : chr "Hatters Way Luton, Luton LU1"
..$ first_published_date: chr "2017-11-16 17:25:36"
..$ last_published_date : chr "2018-01-29 18:40:52"
..$ latitude : chr "51.88188"
..$ listing_id : chr "39336869"
..$ listing_status : chr "sale"
..$ longitude : chr "-0.43237194"
Then I extract sub-elements such as "listing_id" as below:
> id1 <- sapply(listings_England, "[[", "listing_id")
Error in FUN(X[[i]], ...) : subscript out of bounds
> id3 <- sapply(listings_England[1:100000], "[[", "listing_id")
Error in FUN(X[[i]], ...) : subscript out of bounds
> id2 <- sapply(listings_England[1:50000], "[[", "listing_id")
>
> listings_England$listing_id
NULL
>
As you can see, it only works for the last one (same problem for the purrr::map family functions). I was wondering if it the limitation of these functions. And my current solution is:
id <- sapply(listings_England, function(x) x["listing_id"]) %>% as.numeric()
The problem here is "[[" or "$" function is not working for this large list, and only "[" works.
1:50000
but not1:100000
, I bet there's an element between in the50000:100000
range that doesn't have alisting_id
property, or the whole thing isNULL
. – Jesse Tweedleenframe
orbind_rows
either directly or in some combination withmap
). – Jesse Tweedlebind_rows(listings_England)
right away, or maybepurrr:discard(listings_England, is.null)
to dropNULL
elements right away. – Jesse Tweedle