This may have been already solved somewhere else, but I can't find any specific link, so I'll be happy to see a "duplicated" tag...
I have a dataframe with rows that go like the following one:
x y z lon lat count
1 A B C 0 0 3
2 B D Q 1 2 2
Now, to plot data with ggmap
(I'm new and still learning about the grammar of graphics), specifically using the stat_bin2d
I think that I should have to transform my above data in the following way:
x y z lon lat
1 A B C 0 0
2 A B C 0 0
3 A B C 0 0
4 B D Q 1 2
5 B D Q 1 2
Questions:
1) Is my assumption correct?
2) How can I reach my goal?
I've tried several ways to use rbind
without a for
loop, but I didn't solve my problem... The only way that I can think in my little knowledge of R language is something on the line of
my_df <- structure(list(x = structure(1:2, .Label = c("A", "B"), class = "factor"),
y = structure(1:2, .Label = c("B", "D"), class = "factor"),
z = structure(1:2, .Label = c("C", "Q"), class = "factor"),
lon = c(0, 1), lat = c(0, 2), count = c(3, 2)),
.Names = c("x", "y", "z", "lon", "lat", "count"),
row.names = 1:2, class = "data.frame")
for (i in 1:nrow(my_df)){
for (j in 1:(my_df$count[i]-1)){
my_df <- rbind(my_df, my_df[i,])}}
row.names(my_df) <- 1:nrow(my_df)
my_df <- my_df[,1:5]
Result is:
x y z lon lat
1 A B C 0 0
2 B D Q 1 2
3 A B C 0 0
4 A B C 0 0
5 B D Q 1 2
It works, but I'd like to learn a better way to reach my goal.