'R - Cutting a data frame by two columns
I am trying to categorise location points according to their distance and bearing from the first point in a data frame. This code is working, however it only checks the distances from df$dist column.
df$circle_id = cut(df$dist, seq(0,41000000,1000), labels=FALSE, include.lowest=TRUE)
What I want to do is also check if the bearing of points is the same, and only then they can be put in the same category. Bearings are stored in df$bearing.
Simply put, I need to combine these two operations into one:
df$circle_id = cut(df$dist, seq(0,41000000,1000), include.lowest=TRUE)
df$circle_id = cut(df$bearing, seq(-180,180,10), labels=FALSE, include.lowest=TRUE)
Sample data frame:
latitude longitude sensor_time circle_id segment_id weight dist bearing
1 48.15144 17.07569 1447149703 1 1 2 0.000000 -180.00000
3 48.15404 17.07452 1447149743 3 1 1 302.422843 163.22826
4 48.15277 17.07514 1447149762 2 1 1 153.179367 164.50437
5 48.15208 17.07538 1447149771 1 1 1 74.666669 161.91792
6 48.15461 17.07560 1447149773 3 1 3 353.106770 178.87100
9 48.15139 17.07562 1447149811 1 1 2 7.828957 43.83916
Any help is appreciated, thank you
Solution 1:[1]
When you set the labels parameter to FALSE, the cut-function will return integers. An example:
> x <- sample(1:10)
> cut(x, seq(0,10,2))
[1] (6,8] (8,10] (2,4] (4,6] (2,4] (0,2] (8,10] (0,2] (6,8] (4,6]
Levels: (0,2] (2,4] (4,6] (6,8] (8,10]
> cut(x, seq(0,10,2), labels = FALSE)
[1] 4 5 2 3 2 1 5 1 4 3
Solution 2:[2]
I have made the two cuts sequentially and then ordered by cut1, cut2, setting labels=FALSE.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | h3rm4n |
| Solution 2 | fchianucci |
