R 按列名将数据帧的列排序为日期

R 按列名将数据帧的列排序为日期,r,sorting,date,dataframe,R,Sorting,Date,Dataframe,问题中也处理了类似的问题 我有一个以日期为列名的数据框 test = data.frame("01-Apr-16" = c(0, 2, 4, 7, 8), "01-Jan-16" = c(4, 2, 4, 7, 8), "01-Dec-16" = c(1, 3, 8, 3, 2)) 我已将日期转换为相应的数字格式 new_names = apply(data.frame(names(test)), 1, functio

问题中也处理了类似的问题 我有一个以日期为列名的数据框

test = data.frame("01-Apr-16" = c(0, 2, 4, 7, 8), 
                  "01-Jan-16" = c(4, 2, 4, 7, 8), 
                  "01-Dec-16" = c(1, 3, 8, 3, 2))
我已将日期转换为相应的数字格式

new_names = apply(data.frame(names(test)), 1, function(x) as.Date(strptime(x,format = "%d-%b-%y")))
colnames(test) = new_names  
提供的解决方案
test[,order(names(test))]
不起作用。 这个问题有没有有效的解决办法


我从一个外部.csv文件中读取了数据框,该文件在列名中保留了日期的原始格式

您不需要
应用

i1 <- as.Date(names(test), format = 'X%d.%b.%y')
test[order(i1)]

您不需要
应用

i1 <- as.Date(names(test), format = 'X%d.%b.%y')
test[order(i1)]