Answer the question
In order to leave comments, you need to log in
How to "fill" the names in a column with already known names from the table?
Hello, I have a table like this:
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
andgene0 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)
Answer the question
In order to leave comments, you need to log in
I don’t know how relevant this is, but here is a working version (I don’t pretend to be optimal):
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question