R Code: I am looking for some help sorting spatial locations to assign what amounts to a house and address in a new column. I have tried a million combos of “for, ifelse, if else, foreach, while” loops. I have several csv files, which I have appended and then added some extra columns. Specifically, I added a TRUE/FALSE 1/0 for Age==Senior to identify a new home location. The goal is to assign the same house number whenever a 1 appears and keep that house number until the next 1. Once all house numbers are assigned, I want to generate a median of all lat/long at each building location and assin that median lat/long in a new column. The median helps with my GPS jumps per building location. I'm stuck with just counting the homes. First Table:
Latitude | Longitude | Age | Spatial_id | District |
---|---|---|---|---|
5.582719 | -0.1596583 | Senior | 1 | tc01 |
5.582721 | -0.1596585 | Adult | 0 | tc01 |
5.588345 | -0.1656207 | Senior | 1 | tc01 |
5.588341 | -0.1656206 | Adult | 0 | tc01 |
5.588342 | -0.1656202 | Adult | 0 | tc01 |
5.588348 | -0.1656203 | Child | 0 | tc01 |
5.588219 | -0.1653842 | Senior | 1 | tc01 |
5.588219 | -0.1653842 | Adult | 0 | tc01 |
5.588225 | -0.1653841 | Child | 0 | tc01 |
5.588226 | -0.1653841 | Child | 0 | tc01 |
spatial.loc <- c()
spatial.bldg <- c()
house.id = 100000
for (i in 1:nrow(merge.tc01)) {
house.id = house.id + 1
if(merge.tc01$spatial_id[i] == 1){
spatial.loc <- append(spatial.loc, house.id)
spatial.bldg <- paste0(merge.tc01$district,"_", spatial.loc)
}
}
Post production trying to obtain.
Latitude | Longitude | Age | Spatial_id | District | spatial_bldg | spatial_x | spatial_y |
---|---|---|---|---|---|---|---|
5.582719 | -0.1596583 | Senior | 1 | tc01 | tc01_100001 | 5.582720 | -0.1596583 |
5.582721 | -0.1596585 | Adult | 0 | tc01 | tc01_100001 | 5.582720 | -0.1596583 |
5.588345 | -0.1656207 | Senior | 1 | tc01 | tc01_100002 | 5.588344 | -0.1656204 |
5.588341 | -0.1656206 | Adult | 0 | tc01 | tc01_100002 | 5.588344 | -0.1656204 |
5.588342 | -0.1656202 | Adult | 0 | tc01 | tc01_100002 | 5.588344 | -0.1656204 |
5.588348 | -0.1656203 | Child | 0 | tc01 | tc01_100002 | 5.588344 | -0.1656204 |
5.588219 | -0.1653842 | Senior | 1 | tc01 | tc01_100003 | 5.588222 | -0.1653841 |
5.588219 | -0.1653842 | Adult | 0 | tc01 | tc01_100003 | 5.588222 | -0.1653841 |
5.588225 | -0.1653841 | Child | 0 | tc01 | tc01_100003 | 5.588222 | -0.1653841 |
5.588226 | -0.1653841 | Child | 0 | tc01 | tc01_100003 | 5.588222 | -0.1653841 |
Thanks for any help you guys have.
This is getting a little closer. I can count the correct number of 1, but it doesnt fill, instead repeats the index.
spatial.loc <- c()
spatial.bldg <- c()
house.id = 100000
for (i in 1:nrow(merge.tc01)) {
house.id = house.id + 1
if(merge.tc01$spatial_id[i] == 1){
spatial.loc <- append(spatial.loc, house.id)
spatial.bldg <- paste0(merge.tc01$district,"_", spatial.loc)
while (merge.tc01$spatial_id == 0) {
spatial.loc <- append(spatial.loc)
spatial.bldg <- paste0(merge.tc01$district,"_", spatial.loc)
}
}
}
spatial.loc
spatial.bldg