我有一個看起來像這樣的資料
library(tidyverse)
df = tibble(gene=c("geneA","geneB","geneC"),
dat1=c(100,100,50),
dat2=c(50,100,20),
dat3=c(10,20,30))
df
#> # A tibble: 3 × 4
#> gene dat1 dat2 dat3
#> <chr> <dbl> <dbl> <dbl>
#> 1 geneA 100 50 10
#> 2 geneB 100 100 20
#> 3 geneC 50 20 30
使用reprex v2.0.2創建于 2022-10-27
我想將除第一列之外的所有列的值匯總為新列中的串列,同時保留其余列。我希望我的資料看起來像這樣
#> # A tibble: 3 × 4
#> gene dat1 dat2 dat3 data
#> <chr> <dbl> <dbl> <dbl> <list>
#> 1 geneA 100 50 10 100,50,10
#> 2 geneB 100 100 20 100,100,20
#> 3 geneC 50 20 30 50,20,30
uj5u.com熱心網友回復:
rowwise和c_across:_
df %>%
rowwise() %>%
mutate(data = list(c_across(-gene)))
gene dat1 dat2 dat3 data
1 geneA 100 50 10 100, 50, 10
2 geneB 100 100 20 100, 100, 20
3 geneC 50 20 30 50, 20, 30
uj5u.com熱心網友回復:
使用pmap
library(dplyr)
library(purrr)
df %>%
mutate(data = pmap(across(starts_with('dat')), c))
-輸出
# A tibble: 3 × 5
gene dat1 dat2 dat3 data
<chr> <dbl> <dbl> <dbl> <list>
1 geneA 100 50 10 <dbl [3]>
2 geneB 100 100 20 <dbl [3]>
3 geneC 50 20 30 <dbl [3]>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521658.html
上一篇:在R中子集串列子元素的最快方法
