按列中的行值和R中的行中的列值对数据进行排序

时间:2015-08-23 00:46:03

标签: r dataframe

我有六只鹦鹉鸟,"Beefy", "Scoundrel", "Baston", "Mattdamon", "Jesus", and "Hulkhogan"。这些鸟很多,并且在很多不同的地方。我已经决定追踪过去两周内这种情况发生的地点和频率,我试图找出这些小恶魔今天最多的地方。

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行,以便轻松找到今天的poos:

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

但是,如何按今天的日期获得的值对列进行排序,以便我可以快速查看mydata的左上角并找到问题的答案?你能用一行吗?

1 个答案:

答案 0 :(得分:3)

我认为这可能是你想要的。我们只是按第一行排序列(第二行到第二行)。

<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="panel-body navbar-fixed-top" style="background:rgb(200,255,200);">
	aaaa, bbbb, ccc
</div>

<div>
	<div style="margin-top: 50px;">content 1</div>
	<div>content 2</div>
	<div>content 3</div>
	<div>content 4</div>
	<div>content 5</div>
	<div>content 6</div>
	<div>content 7</div>
	<div>content 8</div>
	<div>content 9</div>
	<div>content 10</div>
	<div>content 11</div>
	<div>content 12</div>
	<div>content 13</div>
	<div>content 14</div>
	<div>content 15</div>
	<div>content 16</div>
	<div>content 17</div>
	<div>content 18</div>
	<div>content 19</div>
	<div>content 20</div>
	<div>content 21</div>
	<div>content 22</div>
	<div>content 23</div>
	<div>content 24</div>
	<div>content 25</div>
	<div>content 26</div>
	<div>content 27</div>
	<div>content 28</div>
	<div>content 29</div>
	<div>content 30</div>
	<div>content 31</div>
	<div>content 32</div>
	<div>content 33</div>
	<div>content 34</div>
	<div>content 35</div>
	<div>content 36</div>
	<div>content 37</div>
	<div>content 38</div>
	<div>content 39</div>
	<div>content 40</div>
	<div>content 41</div>
	<div>content 42</div>
	<div>content 43</div>
	<div>content 44</div>
	<div>content 45</div>
	<div>content 46</div>
	<div>content 47</div>
	<div>content 48</div>
	<div>content 49</div>
	<div>content 50</div>
	<div>content 51</div>
	<div>content 52</div>
	<div>content 53</div>
	<div>content 54</div>
	<div>content 55</div>
	<div>content 56</div>
	<div>content 57</div>
	<div>content 58</div>
	<div>content 59</div>

</div>

现在,左上角的值具有今天日期的最高值。

根据评论中的请求,我们可以使用

在一次调用中订购行和列
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