Hi all,
A few times recently, I’ve converted a matrix from sapply
to a dataframe, and ended up with a dataframe where each column is a vector of lists. Why is this happening? Here is my most recent example:
> library(ggmap)
> workshops = paste(c("Yolo", "Merced", "Marin", "Tulare", "San Diego", "Siskiyou", "Imperial"), "County")
> wsLocs = sapply(workshops, geocode)
> locs = as.data.frame(t(wsLocs))
> str(locs)
'data.frame': 7 obs. of 2 variables:
$ lon:List of 7
..$ Yolo County : num -122
..$ Merced County : num -121
..$ Marin County : num -123
..$ Tulare County : num -119
..$ San Diego County: num -117
..$ Siskiyou County : num -123
..$ Imperial County : num -115
$ lat:List of 7
..$ Yolo County : num 38.8
..$ Merced County : num 37.2
..$ Marin County : num 38.1
..$ Tulare County : num 36.1
..$ San Diego County: num 32.7
..$ Siskiyou County : num 41.8
..$ Imperial County : num 33
--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.
Thanks, Vince. do.call(rbind, lapply())
is a fine approach, but I’m still not sure why this is happening. wsLocs
looks like an ordinary numeric matrix, why does as.data.frame
make a list of lists rather than a list of numeric vectors?
--
Michael Levy
c: 304-376-4523
I see, thanks. I’m not in the habit of using str
on matrices… I checked its class (matrix) and the class of each element in it (numeric) but missed that each element was a list. Also to blame: the geocode function is vectorized, so I shouldn’t have been using sapply
on it in the first place. Thanks for the explanations,
Michael