循环和 If 语句来填充 R 中的家庭位置

Loops and If statements to populate home locations in R

R 代码:我正在寻找一些帮助来对空间位置进行排序,以在新列中分配相当于房屋和地址的金额。我已经尝试了一百万个“for、ifelse、if else、foreach、while”循环组合。我有几个 csv 文件,我附加了这些文件,然后添加了一些额外的列。具体来说,我为 Age==Senior 添加了一个 TRUE/FALSE 1/0 来标识新的家庭位置。目标是在出现 1 时分配相同的门牌号,并保持该门牌号直到下一个 1。分配完所有门牌号后,我想在每个建筑物位置生成所有 lat/long 的中位数,并将其分配给新列中的中位数 lat/long。中位数有助于我在每个建筑物位置进行 GPS 跳转。我坚持只数房子。 首先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制作试图获得。

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

感谢你们的帮助。

这越来越近了。我可以数出 1 的正确数量,但它没有填满,而是重复索引。

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

您可以查看 differences 增长 1 的位置,取 cumsum 并输入 sprintf

d <- transform(d, spatial_bldg=sprintf("%s_1%05d", District, 
                                       cumsum(diff(c(0, Spatial_id)) == 1)))
d
#    Latitude  Longitude    Age Spatial_id District spatial_bldg
# 1  5.582719 -0.1596583 Senior          1     tc01  tc01_100001
# 2  5.582721 -0.1596585  Adult          0     tc01  tc01_100001
# 3  5.588345 -0.1656207 Senior          1     tc01  tc01_100002
# 4  5.588341 -0.1656206  Adult          0     tc01  tc01_100002
# 5  5.588342 -0.1656202  Adult          0     tc01  tc01_100002
# 6  5.588348 -0.1656203  Child          0     tc01  tc01_100002
# 7  5.588219 -0.1653842 Senior          1     tc01  tc01_100003
# 8  5.588219 -0.1653842  Adult          0     tc01  tc01_100003
# 9  5.588225 -0.1653841  Child          0     tc01  tc01_100003
# 10 5.588226 -0.1653841  Child          0     tc01  tc01_100003
# 11 5.587270 -0.1743943 Senior          1     tc01  tc01_100004
# 12 5.587271 -0.1743942  Adult          0     tc01  tc01_100004
# 13 5.587270 -0.1743947  Child          0     tc01  tc01_100004
# 14 5.587282 -0.1743944  Child          0     tc01  tc01_100004
# 15 5.587273 -0.1743942  Adult          0     tc01  tc01_100004
# 16 5.587273 -0.1743941  Child          0     tc01  tc01_100004

sprintf 由一个字符向量和任意数量的其他对象、字符串或数字构成。在字符向量中,您可以一个接一个地构建其他对象,并使用 %d 表示整数和 %s 表示它们的位置来标记字符串。您可以在帮助页面 ?sprintf.

中查找更多可用类型

可能你有不同的区,然后查看ave


数据:

d <- read.csv("https://github.com/tnewton2/Rproject_tnewton2/files/6050112/merge_tc01.txt")