对于二级邻居,按行重复到缺失

发布于 2025-01-11 08:17:21 字数 1745 浏览 0 评论 0原文

我可能只是没有找到正确的搜索词,但如果此条目出现在同一行之前,我想删除条目(设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫人生路 2025-01-18 08:17:21

为了获得所需的输出,我们可以这样做:

df1 <- t(apply(df, 1, function(x) replace(x, duplicated(x), NA)))

x <- df1 %>% 
  as_tibble() %>% 
  pivot_longer(
    everything()
  ) %>%
  group_by(value) %>% 
  mutate(id = row_number()-1,
         value = paste0("X.",value,"."),
         value = ifelse(value == "X.NA." & id > 0, paste0(NA, "..", id), value),
         value = ifelse(value == "X.NA.", NA, value)) %>% 
  select(-id) %>% 
  mutate(value = str_replace(value, " ", ".")) %>% 
  pivot_wider(
    names_from = name,
    values_from = value
  )

colnames(df1) <- x

df1
     X.Ashanti. X.Brong.Ahafo. X.Central. X.Eastern. X.Western. <NA> NA..1 NA..2 X.Northern. X.Volta. NA..3
[1,] "Ashanti"  "Brong Ahafo"  "Central"  "Eastern"  "Western"  NA   NA    NA    "Northern"  "Volta"  NA   

To get the desired output we could do:

df1 <- t(apply(df, 1, function(x) replace(x, duplicated(x), NA)))

x <- df1 %>% 
  as_tibble() %>% 
  pivot_longer(
    everything()
  ) %>%
  group_by(value) %>% 
  mutate(id = row_number()-1,
         value = paste0("X.",value,"."),
         value = ifelse(value == "X.NA." & id > 0, paste0(NA, "..", id), value),
         value = ifelse(value == "X.NA.", NA, value)) %>% 
  select(-id) %>% 
  mutate(value = str_replace(value, " ", ".")) %>% 
  pivot_wider(
    names_from = name,
    values_from = value
  )

colnames(df1) <- x

df1
     X.Ashanti. X.Brong.Ahafo. X.Central. X.Eastern. X.Western. <NA> NA..1 NA..2 X.Northern. X.Volta. NA..3
[1,] "Ashanti"  "Brong Ahafo"  "Central"  "Eastern"  "Western"  NA   NA    NA    "Northern"  "Volta"  NA   
枉心 2025-01-18 08:17:21
is.na(df)<-duplicated(as.list(df))
df
       id       nbr_1   nbr_2   nbr_3   nbr_4 nbr_5 scdnbr_1 scdnbr_2 scdnbr_3 scdnbr_4
1 Ashanti Brong Ahafo Central Eastern Western    NA       NA       NA Northern    Volta
  scdnbr_5
1       NA
is.na(df)<-duplicated(as.list(df))
df
       id       nbr_1   nbr_2   nbr_3   nbr_4 nbr_5 scdnbr_1 scdnbr_2 scdnbr_3 scdnbr_4
1 Ashanti Brong Ahafo Central Eastern Western    NA       NA       NA Northern    Volta
  scdnbr_5
1       NA
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文