作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在“SampleFileName”列中有一个表“inputdf”,样本名称以随机顺序排列。
> colnames(inputdf)
[1] "Dye/SamplePeak" "SampleFileName" "Marker" "Allele" "Size" "Height"
[7] "Area" "DataPoint" "flank" "correction" "start" "end"
[13] "control" "iithreshold" "CAG"
library(tidyr)
height <- spread(inputdf, key=SampleFileName, value=Height, fill = 0, convert = FALSE) #Extract heights into separate columns for each sample
> colnames(height)
[1] "Dye/SamplePeak" "Marker"
[3] "Allele" "Size"
[5] "Area" "DataPoint"
[7] "flank" "correction"
[9] "start" "end"
[11] "control" "iithreshold"
[13] "CAG" "A01_MF20170522_FA_A01_2017-05-22_1.fsa"
[15] "A01_MF20170623_FA_A01_2017-06-23_1.fsa" "A02_MF20170623_FA_A02_2017-06-23_1.fsa"
[17] "A03_MF20170623_FA_A03_2017-06-23_1.fsa" "A05_MF20170623_FA_A05_2017-06-23_1.fsa"
[19] "A06_MF20170623_FA_A06_2017-06-23_1.fsa" "A07_MF20170623_FA_A07_2017-06-23_1.fsa"
[21] "A08_MF20170623_FA_A08_2017-06-23_1.fsa" "A09_MF20170623_FA_A09_2017-06-23_1.fsa"
[23] "A10_MF20170623_FA_A10_2017-06-23_1.fsa" "A11_MF20170623_FA_A11_2017-06-23_1.fsa"
[25] "A12_MF20170623_FA_A12_2017-06-23_1.fsa" "B01_MF20170623_FA_B01_2017-06-23_1.fsa"
[27] "B02_MF20170623_FA_B02_2017-06-23_1.fsa" "B03_MF20170623_FA_B03_2017-06-23_1.fsa"
[29] "B04_MF20170623_FA_B04_2017-06-23_1.fsa" "B05_MF20170623_FA_B05_2017-06-23_1.fsa"
[31] "B06_MF20170623_FA_B06_2017-06-23_1.fsa" "B07_MF20170623_FA_B07_2017-06-23_1.fsa"
[33] "B08_MF20170522_FA_B08_2017-05-22_1.fsa" "B08_MF20170623_FA_B08_2017-06-23_1.fsa"
[35] "C01_MF20170623_FA_C01_2017-06-23_1.fsa" "C02_MF20170529_FA_C02_2017-05-30_1.fsa"
[37] "C02_MF20170623_FA_C02_2017-06-23_1.fsa" "C05_MF20170623_FA_C05_2017-06-23_1.fsa"
[39] "C07_MF20170623_FA_C07_2017-06-23_1.fsa" "C08_MF20170623_FA_C08_2017-06-23_1.fsa"
[41] "C09_MF20170623_FA_C09_2017-06-23_1.fsa" "C10_MF20170623_FA_C10_2017-06-23_1.fsa"
[43] "C11_MF20170623_FA_C11_2017-06-23_1.fsa" "C12_MF20170623_FA_C12_2017-06-23_1.fsa"
[45] "D02_MF20170623_FA_D02_2017-06-23_1.fsa" "D03_MF20170623_FA_D03_2017-06-23_1.fsa"
[47] "D04_MF20170623_FA_D04_2017-06-23_1.fsa" "D05_MF20170623_FA_D05_2017-06-23_1.fsa"
[49] "D06_MF20170623_FA_D06_2017-06-23_1.fsa" "D08_MF20170623_FA_D08_2017-06-23_1.fsa"
[51] "D10_MF20170623_FA_D10_2017-06-23_1.fsa" "D11_MF20170623_FA_D11_2017-06-23_1.fsa"
[53] "D12_MF20170623_FA_D12_2017-06-23_1.fsa"
最佳答案
您可以使用 dplyr::select()
之后 tidyr::spread()
保留原始列顺序。请注意,您的行可能仍按字母顺序排序。
library(tidyverse)
mtcars <- rownames_to_column(mtcars, "vehicle")
colnames(mtcars)
# [1] "vehicle" "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
spreadMtcarsSorted <- gather(mtcars, measure, value, -vehicle) %>%
spread(measure, value)
colnames(spreadMtcarsSorted)
# [1] "vehicle" "am" "carb" "cyl" "disp" "drat" "gear" "hp" "mpg" "qsec" "vs" "wt"
spreadMtcarsOriginalColOrder <- gather(mtcars, measure, value, -vehicle) %>%
spread(measure, value) %>%
select(colnames(mtcars)) # use original column order before returning new data frame
colnames(spreadMtcarsOriginalColOrder)
# [1] "vehicle" "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
关于r - 如何停止 tidyr 按字母顺序排列排序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44952959/
我是一名优秀的程序员,十分优秀!