可能是一个简单的
我有这种类型的data.frame
:
df <- data.frame(sp1.name = c("sp1.n1",NA,"sp1.n3",NA), sp1.id = c("sp1.id1","sp1.id2",NA,NA),
sp2.name = c(NA,NA,"sp2.n3",NA), sp2.id = c(NA,NA,NA,"sp2.id4"),
sp3.name = c("sp3.n1",NA,NA,NA), sp3.id = c("sp3.id1",NA,NA,NA))
它由每个“ sp”索引的成对的列组成:sp<index>.name
和sp<index>.id
。在此示例中,索引为1,2,3。
我正在寻找一种方法(可能通过tidyverse
)来为每个sp
合并其对应的名称和ID列对,其中合并规则为:
if !is.na(sp<index>.name) & !is.na(sp<index>.id) return sp<index>.name
if !is.na(sp<index>.name) & is.na(sp<index>.id) return sp<index>.name
else if is.na(sp<index>.name) & !is.na(sp<index>.id) return sp<index>.id
else return NA
因此,在此示例中,生成的data.frame
为:
df <- data.frame(sp1 = c("sp1.n1","sp1.id2","sp1.n3",NA),
sp2 = c(NA,NA,"sp2.n3","sp2.id4"),
sp3 = c("sp3.n1",NA,NA,NA))
答案 0 :(得分:2)
您可以这样做:
library(tidyverse)
df %>%
mutate(rn = row_number()) %>%
gather(id, value, -rn) %>%
mutate(idx = gsub("\\..*", "", id)) %>%
group_by(idx, rn) %>%
mutate(
value = case_when(
any(grepl("name", id) & !is.na(value)) & any( (grepl("id", id) & !is.na(value)) | (grepl("id", id) & is.na(value)) ) ~ value[grepl("name", id)],
any(grepl("name", id) & is.na(value)) & any(grepl("id", id) & !is.na(value)) ~ value[grepl("id", id)],
TRUE ~ NA_character_)) %>%
distinct(idx, value, rn) %>%
spread(idx, value)
给予:
# A tibble: 4 x 4
# Groups: rn [4]
rn sp1 sp2 sp3
<int> <chr> <chr> <chr>
1 1 sp1.n1 NA sp3.n1
2 2 sp1.id2 NA NA
3 3 sp1.n3 sp2.n3 NA
4 4 NA sp2.id4 NA