我有以下大型数据框列表
Cy3list<-c(AllCy3MeansDNA1bind1uM,AllCy3MeansDNA2bind1uM,AllCy3MeansDNA1bind10nM)
在这些数据框中,有一个名为“摩尔浓度”的常用列。我希望能够将此列更改为数值然后对它们进行排序。我正在使用这个功能
'Cy3list<-c(AllCy3MeansDNA1bind1uM,AllCy3MeansDNA2bind1uM,AllCy3MeansDNA1bind10nM)
lapply(Cy3list,function(x){
x["Molarity"]<-as.numeric(x["Molarity"])
x["Molarity"]<-order(x["Molarity"])
return(x)
})'
我希望保留所有数据帧,因为我希望函数循环遍历数据帧并保持它们,以便于以帧的形式查看。当我运行此循环时,摩尔浓度不是有序的,它按以下顺序排列:
&#34; 10&#34; &#34; 100&#34; &#34; 25&#34; &#34; 5&#34; &#34; 50&#34; &#34; 10&#34; &#34; 100&#34;
&#34; 25&#34; &#34; 5&#34; &#34; 50&#34; &#34; 10&#34; &#34; 100&#34; &#34; 25&#34; &#34; 5&#34;
&#34; 50&#34; &#34; 10&#34; &#34; 100&#34; &#34; 25&#34; &#34; 5&#34; &#34; 50&#34; &#34;控制与#34; &#34;控制与#34; &#34;控制与#34; &#34;控制与#34;
请注意,它是按第一个数字排序而不是整数。 任何帮助,将不胜感激。 谢谢
答案 0 :(得分:0)
你可以用这个来做:
构建您的数据:
AllCy3MeansDNA1bind1uM <- data.frame(Molarity=c("10" ,"100", "25", "5" ,"50", "10" ,"100",
"25", "5", "50", "10", "100", "25", "5",
"50", "10", "100", "25", "5", "50", "Control", "Control", "Control",
"Control"), fake_col = rep(1,24) , stringsAsFactors=F)
AllCy3MeansDNA2bind1uM <- data.frame(Molarity=c("10" ,"100", "25", "5" ,"50", "10" ,"100",
"25", "5", "50", "10", "100", "25", "5",
"50", "10", "100", "25", "5", "50", "Control", "Control", "Control",
"Control"), fake_col = rep(1,24), stringsAsFactors=F)
AllCy3MeansDNA1bind10nM <- data.frame(Molarity=c("10" ,"100", "25", "5" ,"50", "10" ,"100",
"25", "5", "50", "10", "100", "25", "5",
"50", "10", "100", "25", "5", "50", "Control", "Control", "Control",
"Control"), fake_col = rep(1,24), stringsAsFactors=F)
将它们存储在列表中(请注意,我使用list
函数而不是c
)
Cy3list<-list(AllCy3MeansDNA1bind1uM=AllCy3MeansDNA1bind1uM,AllCy3MeansDNA2bind1uM=AllCy3MeansDNA2bind1uM,
AllCy3MeansDNA1bind10nM=AllCy3MeansDNA1bind10nM)
然后试试这个function
:
library(gtools) #you need to load this library for mixedsort
new_df<-lapply(Cy3list,function(x) {
x["Molarity"]<-mixedsort(x[["Molarity"]]) #notice the double brackets
return(x)
})
我不知道您是否需要在Molarity列中包含数字数据(当您在其中也有字符数据时没有意义)。如果你真的需要使用它:
x["Molarity"]<- as.numeric(mixedsort(x[["Molarity"]]))
循环中的但请注意所有的角色数据都将转换为NA,你也会得到相应的警告。
验证代码是否有效:
> new_df$AllCy3MeansDNA1bind1uM[['Molarity']]
[1] "5" "5" "5" "5" "10" "10" "10" "10" "25" "25" "25"
[12] "25" "50" "50" "50" "50" "100" "100" "100" "100" "Control" "Control"
[23] "Control" "Control"
列是订购的!
希望它有所帮助!