说我有一个看起来像这样的数据框:
gene drug log2FC
Ubb Naloxone 0.6375514
Tuba1a Naloxone 0.5827224
Scd1 Naloxone -0.7249997
Ubb Aspirin 0.8000
Tuba1a Aspirin 0.73324
Scd1 Aspirin 0.2497
Ubb Haldol 0.0375
Tuba1a Haldol 0.25824
Scd1 Haldol -0.0249997
是否有一种简单的方法可以为每种独特的药物创建列,所以我留下了这样的内容:
gene Naloxone_log2FC Asirin_Log2FC Haldol_log2FC
Ubb 0.6375514 0.73324 0.0375
Tuba1a ...
Scd1 ...
Thanks!
答案 0 :(得分:5)
您可以使用较新的tidyr::spread()
中的tidyr::pivot_wider()
:
library(tidyr)
data <- read.table(h=T, strin=F, text="gene drug log2FC
Ubb Naloxone 0.6375514
Tuba1a Naloxone 0.5827224
Scd1 Naloxone -0.7249997
Ubb Aspirin 0.8000
Tuba1a Aspirin 0.73324
Scd1 Aspirin 0.2497
Ubb Haldol 0.0375
Tuba1a Haldol 0.25824
Scd1 Haldol -0.0249997")
data %>% spread(drug, log2FC)
#> gene Aspirin Haldol Naloxone
#> 1 Scd1 0.24970 -0.0249997 -0.7249997
#> 2 Tuba1a 0.73324 0.2582400 0.5827224
#> 3 Ubb 0.80000 0.0375000 0.6375514
data %>% pivot_wider(names_from = "drug", values_from = log2FC)
#> # A tibble: 3 x 4
#> gene Naloxone Aspirin Haldol
#> <chr> <dbl> <dbl> <dbl>
#> 1 Ubb 0.638 0.8 0.0375
#> 2 Tuba1a 0.583 0.733 0.258
#> 3 Scd1 -0.725 0.250 -0.0250
由reprex package(v0.3.0)于2019-11-11创建
答案 1 :(得分:2)
您可以使用spread
包的tidyr
函数(我假设您的数据框为df
)
library("tidyr")
df = spread(df, drug, log2FC)
要将“ _log2FC”附加到列名,可以使用:
for (i in 2:length(colnames(df))) {
colnames(df)[i] = paste0(colnames(df)[i], "_log2FC")
}