I have a list of around 200,000 elements.
Each element stores two values and represents map coordinates (latitude and longitude).
I want to extract the values into lat
and lon
variables and so far have come up with this:
for(i in nrow(users)) {
lat[i] <- users$location[[i]][1]
lon[i] <- users$location[[i]][2]
}
coords <- as.data.frame(cbind(lat, lon))
As far as I can see, it appears to have extracted the first element and then 19 elements at the end with nothing between (20 in total when checking with complete.cases
).
Ideally, I would like to exclude NA
and 0, 0
values also.
Looking at the list directly, I can see that this is wrong as there are several values contained within it.
If I compare the final data frame to the list items, the figures don't match. For example, the value -73.9924
exists in the list but not in my data frame.
Where am I going wrong?
My final data frame:
> coords[complete.cases(coords), ]
lat lon
1 37.4590 -122.1781
96960 40.8152 -73.3624
96961 40.0409 -75.6374
96962 42.5153 -70.9075
96963 33.7773 -84.3366
96964 39.9831 -86.2876
96965 40.7588 -73.9680
96966 36.7646 -76.1990
96967 44.7415 -91.3012
96968 42.6179 -70.7154
96969 40.5953 -74.6173
96970 50.8000 -0.3667
96971 34.0523 -118.3852
96972 41.4468 -74.0689
96973 26.9467 -80.2170
96974 40.7139 -74.0079
96975 34.2313 -118.1486
96976 43.6655 -79.4378
96977 39.0972 -84.1225
96978 -122.1781 37.4590
Sample of list contents:
[[734]]
[1] 0 0
[[735]]
[1] 0 0
[[736]]
[1] 0 0
[[737]]
[1] 0 0
[[738]]
[1] -73.9924 40.7553
[[739]]
[1] 0 0
[[740]]
[1] -76.7818 39.4370
[[741]]
[1] -97.822 37.751
[[742]]
NULL
[[743]]
[1] 0 0
[[744]]
[1] 0 0