2015-08-23 128 views
2

我有六只鹦鹉鸟,"Beefy", "Scoundrel", "Baston", "Mattdamon", "Jesus", and "Hulkhogan"。这些鸟在很多不同的地方都会呕吐。我已经决定追踪过去两周内这种情况发生的频率,我试图弄清楚今天这些小恶魔在哪里发生的事情。按行中的行数值排列数据,并按列中的值排序R

mydata <- data.frame(Dates = structure(c(16656, 16657, 16658, 16659, 
             16660, 16661, 16662, 16663, 
             16664, 16665, 16666, 16667, 
             16668, 16669 
             ), 
             class = "Date"), 
        PooLoc1 = sample(1:40, 7), 
        PooLoc2 = sample(1:10, 7), 
        PooLoc3 = sample(1:10, 7), 
        PooLoc4 = sample(1:30, 7), 
        PooLoc5 = sample(1:20, 7), 
        PooLoc6 = sample(1:70, 7) 
) 

head(mydata) 
    Dates PooLoc1 PooLoc2 PooLoc3 PooLoc4 PooLoc5 PooLoc6 
2015-08-09  24  3  9  1  16  45 
2015-08-10  39  2  2  12  12  2 
2015-08-11  14  7  6  5  19  4 
2015-08-12  26  9  8  27  3  64 
2015-08-13  20  4  1  15  20  48 
2015-08-14  9  1  4  8  8  61 

我可以为了mydata行柱日期很容易地找到今天的普斯像这样:

mydata <- mydata[order(mydata[["Dates"]], decreasing = TRUE), ] 

但我怎么通过今天的日期获得这样我就可以立马值排序列在mydata的左上角找到我的问题的答案?你能做到一行吗?

回答

3

我认为这可能是你想要的。我们只需在第一行中排列列(第二个到结尾)。

mydata[, c(1, order(mydata[1, -1], decreasing = TRUE) + 1)] 
#  Dates PooLoc6 PooLoc1 PooLoc4 PooLoc5 PooLoc3 PooLoc2 
# 6 2015-08-14  61  9  8  8  4  1 
# 5 2015-08-13  48  20  15  20  1  4 
# 4 2015-08-12  64  26  27  3  8  9 
# 3 2015-08-11  4  14  5  19  6  7 
# 2 2015-08-10  2  39  12  12  2  2 
# 1 2015-08-09  45  24  1  16  9  3 

现在左上角的值是今天日期的最高值。

而且每个注释中的要求,我们可以用

mydata[ 
    order(mydata$Dates, decreasing = TRUE), 
    c(1, order(mydata[1, -1], decreasing = TRUE) + 1) 
] 
+0

由于订购一个呼叫的行和列!是否可以在一行中进行列排序和日期排序? – r3robertson

+1

是的,只要把你的日期顺序调用放在逗号左边,列顺序在右边,像这样 - 'mydata [order(mydata $ Dates,decrease = TRUE),c(1,order(mydata [1 ,-1],递减= TRUE)+ 1)]' –