split
将始终按字典顺序排列拆分。在某些情况下,人们宁愿保持自然秩序。一个人总是可以实现一个手动功能,但是有一个基本的 R 解决方案可以做到这一点吗?
可重现的例子:
输入:
Date.of.Inclusion Securities.Included Securities.Excluded yearmon
1 2013-04-01 INDUSINDBK SIEMENS 4 2013
2 2013-04-01 NMDC WIPRO 4 2013
3 2012-09-28 LUPIN SAIL 9 2012
4 2012-09-28 ULTRACEMCO STER 9 2012
5 2012-04-27 ASIANPAINT RCOM 4 2012
6 2012-04-27 BANKBARODA RPOWER 4 2012
split
输出:
R> split(nifty.dat, nifty.dat$yearmon)
$`4 2012`
Date.of.Inclusion Securities.Included Securities.Excluded yearmon
5 2012-04-27 ASIANPAINT RCOM 4 2012
6 2012-04-27 BANKBARODA RPOWER 4 2012
$`4 2013`
Date.of.Inclusion Securities.Included Securities.Excluded yearmon
1 2013-04-01 INDUSINDBK SIEMENS 4 2013
2 2013-04-01 NMDC WIPRO 4 2013
$`9 2012`
Date.of.Inclusion Securities.Included Securities.Excluded yearmon
3 2012-09-28 LUPIN SAIL 9 2012
4 2012-09-28 ULTRACEMCO STER 9 2012
请注意,yearmon
它已经按我喜欢的特定顺序排序。这可以被认为是给定的,因为如果这不成立,这个问题会被稍微错误地指定。
期望的输出:
$`4 2013`
Date.of.Inclusion Securities.Included Securities.Excluded yearmon
1 2013-04-01 INDUSINDBK SIEMENS 4 2013
2 2013-04-01 NMDC WIPRO 4 2013
$`9 2012`
Date.of.Inclusion Securities.Included Securities.Excluded yearmon
3 2012-09-28 LUPIN SAIL 9 2012
4 2012-09-28 ULTRACEMCO STER 9 2012
$`4 2012`
Date.of.Inclusion Securities.Included Securities.Excluded yearmon
5 2012-04-27 ASIANPAINT RCOM 4 2012
6 2012-04-27 BANKBARODA RPOWER 4 2012
谢谢。
PS:我知道有更好的方法来创建yearmon
以保留该顺序,但我正在寻找一个通用的解决方案。