我有一张桌子" inputdf'列中的随机顺序中的样本名称' SampleFileName'。
> colnames(inputdf)
[1] "Dye/SamplePeak" "SampleFileName" "Marker" "Allele" "Size" "Height"
[7] "Area" "DataPoint" "flank" "correction" "start" "end"
[13] "control" "iithreshold" "CAG"
我正在使用“高度”的tidyr传播结果'列分为不同的列,每列由' SampleFileName'中的值命名。
library(tidyr)
height <- spread(inputdf, key=SampleFileName, value=Height, fill = 0, convert = FALSE) #Extract heights into separate columns for each sample
我的样本在列&#39; SampleFileName&#39;列中按字母顺序排列。而且我想按顺序保留它们。但是,传播会按字母顺序自动排序。我很感激你的帮助!
> 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"
答案 0 :(得分:1)
你可以尝试这样的事情。
library(tidyr)
# Get vector of current column names (excluding "SampleFileName" and "Height" as they will not exist in final dataset) and all of the SampleFileName values.
cols <- c(colnames(inputdf)[!(colnames(inputdf) %in% c("SampleFileName","Height"))], unique(inputdf$SampleFileName))
# Spread the SampleFileName column
height <- spread(inputdf, key=SampleFileName, value=Height, fill = 0, convert = FALSE)
# Select the columns in the order they are listed in the cols vector
height <- height[,cols]
答案 1 :(得分:0)
您可以在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"