gpt4 book ai didi

r - Dplyr:group_by 并将多列转换为向量

转载 作者:行者123 更新时间:2023-12-04 12:50:08 25 4
gpt4 key购买 nike

我有一个关于如何将多列转换为向量的问题。我有以下数据集,我想根据它们的条件对它们进行分组,并将所有位置计数放入一个向量中。我知道我可以使用 as.vector() 单独转换它们,但我想知道是否有 dplyr 方式。谢谢!

test -> structure(list(gene_id = c("gene0", "gene0", "gene0", "gene0", 
"gene0", "gene0", "gene0", "gene0", "gene0", "gene0", "gene0",
"gene0", "gene0", "gene0", "gene0", "gene0", "gene0", "gene0",
"gene0", "gene0", "gene0", "gene0", "gene0", "gene0"), codon_index = c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L), position_1_count = c(2L, 7L, 8L,
0L, 2L, 22L, 19L, 15L, 134L, 1L, 127L, 30L, 0L, 0L, 1L, 4L, 65L,
234L, 1L, 3L, 57L, 0L, 4L, 16L), position_2_count = c(0L, 5L,
5L, 0L, 3L, 2L, 3L, 13L, 134L, 0L, 36L, 5L, 0L, 0L, 0L, 1L, 150L,
7L, 0L, 7L, 7L, 0L, 6L, 1L), position_3_count = c(0L, 2L, 1L,
0L, 4L, 0L, 3L, 32L, 43L, 3L, 9L, 1L, 0L, 0L, 0L, 4L, 105L, 1L,
0L, 14L, 5L, 0L, 6L, 1L), condition = structure(c(1L, 1L, 1L,
7L, 7L, 7L, 3L, 3L, 3L, 5L, 5L, 5L, 8L, 8L, 8L, 2L, 2L, 2L, 4L,
4L, 4L, 6L, 6L, 6L), .Label = c("c", "cup", "n", "nup", "p",
"pup", "min", "rich"), class = "factor")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -24L), .Names = c("gene_id",
"codon_index", "position_1_count", "position_2_count", "position_3_count",
"condition"))

> head(a)
# A tibble: 6 × 6
gene_id codon_index position_1_count position_2_count position_3_count condition
<chr> <int> <int> <int> <int> <fctr>
1 gene0 1 2 0 0 c
2 gene0 2 7 5 2 c
3 gene0 3 8 5 1 c
4 gene0 1 0 0 0 min
5 gene0 2 2 3 4 min
6 gene0 3 22 2 0 min

我们如何将这个数据集转换成(我没有在这里添加列名)

2 0 0 7 5 2 8 5 1 c
0 0 0 2 3 4 22 2 0 min

最佳答案

另一种选择:

library(purrr)

test %>%
slice_rows("condition") %>%
by_slice(function(x) unlist(x[-(1:2)]), .to = "vec")

给出:

#  condition                                vec
#1 c 2, 7, 8, 0, 5, 5, 0, 2, 1
#2 cup 4, 65, 234, 1, 150, 7, 4, 105, 1
#3 n 19, 15, 134, 3, 13, 134, 3, 32, 43
#4 nup 1, 3, 57, 0, 7, 7, 0, 14, 5
#5 p 1, 127, 30, 0, 36, 5, 3, 9, 1
#6 pup 0, 4, 16, 0, 6, 1, 0, 6, 1
#7 min 0, 2, 22, 0, 3, 2, 0, 4, 0
#8 rich 0, 0, 1, 0, 0, 0, 0, 0, 0

正如@advance 在评论中提到的,如果你想要结果按行:

test %>% 
slice_rows("condition") %>%
by_slice(function(x) as.vector(t(x[-(1:2)])), .to = "vec")

# condition vec
#1 c 2, 0, 0, 7, 5, 2, 8, 5, 1
#2 cup 4, 1, 4, 65, 150, 105, 234, 7, 1
#3 n 19, 3, 3, 15, 13, 32, 134, 134, 43
#4 nup 1, 0, 0, 3, 7, 14, 57, 7, 5
#5 p 1, 0, 3, 127, 36, 9, 30, 5, 1
#6 pup 0, 0, 0, 4, 6, 6, 16, 1, 1
#7 min 0, 0, 0, 2, 3, 4, 22, 2, 0
#8 rich 0, 0, 0, 0, 0, 0, 1, 0, 0

或者使用 do() 而不是 summarise() 改编@DavidArenburg 的评论:

test %>% 
group_by(condition) %>%
select(position_1_count:condition) %>%
do(res = c(t(.[,-4])))

给出:

#  condition                                res
#1 c 2, 0, 0, 7, 5, 2, 8, 5, 1
#2 cup 4, 1, 4, 65, 150, 105, 234, 7, 1
#3 n 19, 3, 3, 15, 13, 32, 134, 134, 43
#4 nup 1, 0, 0, 3, 7, 14, 57, 7, 5
#5 p 1, 0, 3, 127, 36, 9, 30, 5, 1
#6 pup 0, 0, 0, 4, 6, 6, 16, 1, 1
#7 min 0, 0, 0, 2, 3, 4, 22, 2, 0
#8 rich 0, 0, 0, 0, 0, 0, 1, 0, 0

关于r - Dplyr:group_by 并将多列转换为向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40093595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com