id
gene0 1 2 3
gene1 0 0 5
gene2 6 8 0
gene3 5 5 5
0 0 5
1 2 3
gene1 0 0 5
и gene0 1 2 3
id
gene0 1 2 3
gene1 0 0 5
gene2 6 8 0
gene3 5 5 5
gene1 0 0 5
gene0 1 2 3
df %>%
group_by_at(-1) %>%
fill(id)
library(tidyverse)
library(vctrs)
con <- textConnection(
"gene0 1 2 3
gene1 0 0 5
gene2 6 8 0
gene3 5 5 5
NA 0 0 5
NA 1 2 3
NA 0 0 5
NA 6 8 0
NA 5 5 5
NA 0 0 5
NA 1 2 3")
read.table(con) %>%
as_tibble %>%
set_names(vec_c("id", "A", "B", "C")) -> src_table
close(con)
src_table %>%
filter(!is.na(id)) -> template
src_table %>%
select(-id) %>%
left_join(template, by = vec_c("A", "B", "C")) %>%
select(id, everything()) -> result
print(src_table)
#> # A tibble: 11 x 4
#> id A B C
#> <fct> <int> <int> <int>
#> 1 gene0 1 2 3
#> 2 gene1 0 0 5
#> 3 gene2 6 8 0
#> 4 gene3 5 5 5
#> 5 <NA> 0 0 5
#> 6 <NA> 1 2 3
#> 7 <NA> 0 0 5
#> 8 <NA> 6 8 0
#> 9 <NA> 5 5 5
#> 10 <NA> 0 0 5
#> 11 <NA> 1 2 3
print(result)
#> # A tibble: 11 x 4
#> id A B C
#> <fct> <int> <int> <int>
#> 1 gene0 1 2 3
#> 2 gene1 0 0 5
#> 3 gene2 6 8 0
#> 4 gene3 5 5 5
#> 5 gene1 0 0 5
#> 6 gene0 1 2 3
#> 7 gene1 0 0 5
#> 8 gene2 6 8 0
#> 9 gene3 5 5 5
#> 10 gene1 0 0 5
#> 11 gene0 1 2 3