对于二级邻居,按行重复到缺失
我可能只是没有找到正确的搜索词,但如果此条目出现在同一行之前,我想删除条目(设置为 NA)。
从 df 开始,我想到达 df2。
df <- data.frame(t(data.frame(c("Ashanti","Brong Ahafo","Central","Eastern","Western",NA,
"Ashanti","Eastern","Northern","Volta","Western"),
c("Brong Ahafo","Ashanti","Eastern","Northern","Volta",
"Western","Brong Ahafo","Central","Eastern","Western",NA))))
rownames(df) <- NULL
names(df) <- c("id","nbr_1","nbr_2","nbr_3","nbr_4","nbr_5","scdnbr_1",
"scdnbr_2","scdnbr_3","scdnbr_4","scdnbr_5")
df2 <- data.frame(t(data.frame(c("Ashanti","Brong Ahafo","Central","Eastern","Western",NA,
NA,NA,"Northern","Volta",NA),
c("Brong Ahafo","Ashanti","Eastern","Northern","Volta","Western",NA,
"Central",NA,NA,NA))))
rownames(df2) <- NULL
names(df2) <- c("id","nbr_1","nbr_2","nbr_3","nbr_4","nbr_5","scdnbr_1",
"scdnbr_2","scdnbr_3","scdnbr_4","scdnbr_5")
可能没有必要,但应用的上下文是使用 poly2nb 命令获取加纳境内的二阶相邻区域,
pacman::p_load("spdep","sp","expp","raster","dplyr","tidyr")
ghana <- getData('GADM', country='GHA', level=1)
# first degree neighbors
nb <- poly2nb(ghana, row.names=ghana$NAME_1)
nb <- neighborsDataFrame(nb)
nb <- nb%>% group_by(id) %>% mutate(nbr = sequence(n())) %>%
spread(key = nbr, value = id_neigh, sep="_")
# second degree neighbors
nb2_2 <- nb2 %>%
rename(scdnbr_1=nbr_1,
scdnbr_2=nbr_2,
scdnbr_3=nbr_3,
scdnbr_4=nbr_4,
scdnbr_5=nbr_5)
nb3 <- nb2 %>%
left_join(nb2_2, by=c("nbr_1"="id"))
然后我将继续加入其余四个一级邻居的二级邻居。 但在这一步之前,我想实现上面描述的目标(如 df 到 df2 中)。
谢谢大家!
I am probably just not hitting the right search terms, but I would like to delete entries (set to NA) if this entry appears before in the same row.
Starting from df I want to get to df2.
df <- data.frame(t(data.frame(c("Ashanti","Brong Ahafo","Central","Eastern","Western",NA,
"Ashanti","Eastern","Northern","Volta","Western"),
c("Brong Ahafo","Ashanti","Eastern","Northern","Volta",
"Western","Brong Ahafo","Central","Eastern","Western",NA))))
rownames(df) <- NULL
names(df) <- c("id","nbr_1","nbr_2","nbr_3","nbr_4","nbr_5","scdnbr_1",
"scdnbr_2","scdnbr_3","scdnbr_4","scdnbr_5")
df2 <- data.frame(t(data.frame(c("Ashanti","Brong Ahafo","Central","Eastern","Western",NA,
NA,NA,"Northern","Volta",NA),
c("Brong Ahafo","Ashanti","Eastern","Northern","Volta","Western",NA,
"Central",NA,NA,NA))))
rownames(df2) <- NULL
names(df2) <- c("id","nbr_1","nbr_2","nbr_3","nbr_4","nbr_5","scdnbr_1",
"scdnbr_2","scdnbr_3","scdnbr_4","scdnbr_5")
Probably not necessary, but the applied context is to get the second order neighboring regions within Ghana with the poly2nb command
pacman::p_load("spdep","sp","expp","raster","dplyr","tidyr")
ghana <- getData('GADM', country='GHA', level=1)
# first degree neighbors
nb <- poly2nb(ghana, row.names=ghana$NAME_1)
nb <- neighborsDataFrame(nb)
nb <- nb%>% group_by(id) %>% mutate(nbr = sequence(n())) %>%
spread(key = nbr, value = id_neigh, sep="_")
# second degree neighbors
nb2_2 <- nb2 %>%
rename(scdnbr_1=nbr_1,
scdnbr_2=nbr_2,
scdnbr_3=nbr_3,
scdnbr_4=nbr_4,
scdnbr_5=nbr_5)
nb3 <- nb2 %>%
left_join(nb2_2, by=c("nbr_1"="id"))
I would then continue joining in the second degree neighbors for the four remaining first neighbors.
But before that step I would like to achieve what I described above (as in df to df2).
Thank you all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了获得所需的输出,我们可以这样做:
To get the desired output we could do: