【发布时间】:2017-05-04 00:33:03
【问题描述】:
我有以下小标题:
df <- tibble::tribble(
~Sample_name, ~CRT, ~SR, ~`Bcells,DendriticCells,Macrophage`,
"S1", 0.079, 0.592, "0.077,0.483,0.555",
"S2", 0.082, 0.549, "0.075,0.268,0.120"
)
df
#> # A tibble: 2 × 4
#> Sample_name CRT SR `Bcells,DendriticCells,Macrophage`
#> <chr> <dbl> <dbl> <chr>
#> 1 S1 0.079 0.592 0.077,0.483,0.555
#> 2 S2 0.082 0.549 0.075,0.268,0.120
请注意,第三列以逗号分隔。如何将df 转换成这种整洁的形式:
Sample_name CRT SR Score Celltype
S1 0.079 0.592 0.077 Bcells
S1 0.079 0.592 0.483 DendriticCells
S1 0.079 0.592 0.555 Macrophage
S2 0.082 0.549 0.075 Bcells
S2 0.082 0.549 0.268 DendriticCells
S2 0.082 0.549 0.120 Macrophage
【问题讨论】:
-
这看起来像是 CSV 没有正确读入。与其在事后尝试修复它,不如从一开始就弄清楚为什么它没有解决它会更容易。
-
要修复读数,可能是
cbind(df[-4], read.csv(text = paste(names(df)[4], paste(df[[4]], collapse = '\n'), sep = '\n'), header = TRUE)),然后您可以轻松地使用tidyr::gather进行重塑。