R:你能指定reshape / cast中变量列的顺序吗?(R: Can you specify the order of variable columns from reshape/cast?)
我正在使用
cast
功能来创建宽格式的数据框。 我希望能够控制使用强制转换所产生的列的顺序。 这可能吗?在下面的示例中,它们是订单:
cogs_xdep, sales, sga
我想订购它们:
sales, cogs_xdep, sga
整个过程实际上是从一个宽格式化框架开始的,我在演员表之前使用
melt
变为长格式。一个例子如下
>rawdata_all coy_name gvkey_iid factor X20130930 X20130831 X20130731 X20130630 X20130531 X20130430 X20130331 X20130228 X20130131 X20121231 1 Coy 1 111111 sales 1 2 3 4 5 6 7 8 9 10 2 Coy 2 22222 sales 2 12 22 32 42 52 62 72 82 92 3 Coy 3 333333 sales 3 103 203 303 403 503 603 703 803 903 4 Coy 1 111111 cogs_xdep 4 5 6 7 8 9 10 11 12 13 5 Coy 2 22222 cogs_xdep 5 15 25 35 45 55 65 75 85 95 6 Coy 3 333333 cogs_xdep 6 106 206 306 406 506 606 706 806 906 7 Coy 1 111111 sga 7 8 9 10 11 12 13 14 15 16 8 Coy 2 22222 sga 8 18 28 38 48 58 68 78 88 98 9 Coy 3 333333 sga 9 109 209 309 409 509 609 709 809 909 ...
融化为长格式
> non_data_cols <- 3 # There are 3 non-value columns > master_long <- melt(rawdata_all, id=1:non_data_cols,measured=(non_data_cols+1):length(rawdata_all)) > master_long coy_name gvkey_iid factor variable value 1 Coy 1 111111 sales X20130930 1 2 Coy 2 22222 sales X20130930 2 3 Coy 3 333333 sales X20130930 3 4 Coy 1 111111 cogs_xdep X20130930 4 5 Coy 2 22222 cogs_xdep X20130930 5 6 Coy 3 333333 cogs_xdep X20130930 6 7 Coy 1 111111 sga X20130930 7 8 Coy 2 22222 sga X20130930 8 ...
最后投射'因素'来创建更广泛的数据框架。 (我还将'变量'重命名为'日期',并从日期值的开头删除'X')。
> master <- cast(master_long, ...~factor) coy_name gvkey_iid date cogs_xdep sales sga 1 Coy 1 111111 20130930 4 1 7 2 Coy 1 111111 20130831 5 2 8 3 Coy 1 111111 20130731 6 3 9 4 Coy 1 111111 20130630 7 4 10 5 Coy 1 111111 20130531 8 5 11 6 Coy 1 111111 20130430 9 6 12 7 Coy 1 111111 20130331 10 7 13 8 Coy 1 111111 20130228 11 8 14 9 Coy 1 111111 20130131 12 9 15 10 Coy 1 111111 20121231 13 10 16 ...
理想情况下,我希望按以下顺序排列最后3列:sales,cogs_xdep,sga。
cast
似乎按字母顺序排列,因为您可以看到它们在原始数据框和长格式数据框中以所需方式排序。任何建议将不胜感激。 虽然仅用3重新排列列更容易,但在30多列的真实情况下更加麻烦。
谢谢,
I'm using the
cast
function to create a wide formatted data frame. I'd like to be able to control the ordering of the columns that result from usingcast
. Is this possible?In the example below, they are order:
cogs_xdep, sales, sga
I would like to order them:
sales, cogs_xdep, sga
The whole process actually starts with a wide formatted frame that I use
melt
to change into a long format before the cast.An example is below
>rawdata_all coy_name gvkey_iid factor X20130930 X20130831 X20130731 X20130630 X20130531 X20130430 X20130331 X20130228 X20130131 X20121231 1 Coy 1 111111 sales 1 2 3 4 5 6 7 8 9 10 2 Coy 2 22222 sales 2 12 22 32 42 52 62 72 82 92 3 Coy 3 333333 sales 3 103 203 303 403 503 603 703 803 903 4 Coy 1 111111 cogs_xdep 4 5 6 7 8 9 10 11 12 13 5 Coy 2 22222 cogs_xdep 5 15 25 35 45 55 65 75 85 95 6 Coy 3 333333 cogs_xdep 6 106 206 306 406 506 606 706 806 906 7 Coy 1 111111 sga 7 8 9 10 11 12 13 14 15 16 8 Coy 2 22222 sga 8 18 28 38 48 58 68 78 88 98 9 Coy 3 333333 sga 9 109 209 309 409 509 609 709 809 909 ...
Melt to put it in long format
> non_data_cols <- 3 # There are 3 non-value columns > master_long <- melt(rawdata_all, id=1:non_data_cols,measured=(non_data_cols+1):length(rawdata_all)) > master_long coy_name gvkey_iid factor variable value 1 Coy 1 111111 sales X20130930 1 2 Coy 2 22222 sales X20130930 2 3 Coy 3 333333 sales X20130930 3 4 Coy 1 111111 cogs_xdep X20130930 4 5 Coy 2 22222 cogs_xdep X20130930 5 6 Coy 3 333333 cogs_xdep X20130930 6 7 Coy 1 111111 sga X20130930 7 8 Coy 2 22222 sga X20130930 8 ...
Finally cast on 'factor' to create wider data frame. (I also renamed 'variable' to 'date' and removed the 'X' from the start of the date values).
> master <- cast(master_long, ...~factor) coy_name gvkey_iid date cogs_xdep sales sga 1 Coy 1 111111 20130930 4 1 7 2 Coy 1 111111 20130831 5 2 8 3 Coy 1 111111 20130731 6 3 9 4 Coy 1 111111 20130630 7 4 10 5 Coy 1 111111 20130531 8 5 11 6 Coy 1 111111 20130430 9 6 12 7 Coy 1 111111 20130331 10 7 13 8 Coy 1 111111 20130228 11 8 14 9 Coy 1 111111 20130131 12 9 15 10 Coy 1 111111 20121231 13 10 16 ...
I would ideally like to have the final 3 columns in the following order: sales, cogs_xdep, sga.
cast
appears to have arranged them alphabetically as you can see they are ordered in the desired way in both the original data frame and the long formatted data frame.Any suggestions would be greatly appreciated. While it is easier enough to rearrange the columns with only 3, it's more cumbersome in the real situation of 30+ columns.
Thanks,
原文:https://stackoverflow.com/questions/18848947
满意答案
我一直在寻找这个已经很长一段时间试图弄清楚发生了什么,因为这对我来说非常有趣。 这就是我的结论:
事实上,正如你所看到的那样,文本正在向边缘运行,但是通过调用
inline
它只是表示容器应该更小的错觉,因为它强制将consectetr
这个词强加到下一行。 我注意到的一些事情(我删除了你的内联CSS并将其添加到JsFiddle的CSS部分以获得更好的可视性):原始代码: 原始的FIDDLE
1.将
p
设置为inline-block
或inline
并添加分word-break: break-all
您可以看到consectetr
这个词完全适合框中,正好在边缘上方:2.如果您使用原始代码并将
p
s父div
更改为inline
而不是inline-block
,则可以看到文本周围有红色边框,但它不会包装容器,它会包装文本字符串,甚至打破句子的作用:3.最后,如果您要添加换行符,结果将如您所愿:
所以我的最后结论是,虽然看起来字符串被分成两条不同的线并且小于它的父宽度,但这只是一种幻觉,它实际上仍然占据了它的父级的全宽,除非一个标签专门告诉它在哪里打破阵容。
I've been looking this over for quite some time trying to figure out what's going on because this is very intriguing to me. And this is what I have concluded:
The text is in fact running to the edge as you see, but by calling
inline
it's merely presenting the illusion that the container should be smaller because it forced the wordconsectetr
to the next line. A few things I have noticed (I removed your inline CSS and added it to JsFiddle's CSS section for better view-ability):ORIGINAL CODE: ORIGINAL FIDDLE
1. When setting the
p
toinline-block
orinline
and addingword-break: break-all
you can see the wordconsectetr
perfectly fits into the box, right above the edge:2. If you use your original code and change the
p
s parentdiv
toinline
instead ofinline-block
you can see the red border wraps around the text, but it doesn't wrap the container, it wraps the string of text, even breaking where the sentence does:3. And finally if you were to add in a line-break, the results would be as you expected:
So my final conclusion is although it appears the string is broken up into two different lines and smaller than it's parent width, it's just an illusion and it's actually still taking up the full width of it's parent unless a
<br/>
tag specifically tells it where to break the line up.
相关问答
更多如何使带换行符的内联元素的背景延伸到每一边的最边缘?(How to make the background of an inline element with a line break extend to the farthest edge of each side?)
就换行符而言,内联块与内联元素(Inline-block vs inline elements in terms of line-breaks)
如何使用float而不是换行符来扩展元素并将其扩展到父容器之外?(How do I make elements with float not line break and extend outside their parent container?)
获取具有不同背景颜色的两列,这些颜色延伸到屏幕边缘[重复](Get Two Columns with different background colours that extend to screen edge [duplicate])
带换行符的内联段落元素不会保持内联(inline paragraph element with line break does not remain inline)
将背景延伸到边距(Extend a background into the margin)
内联换行符?(Inline line breaks?)
如何使我的边界一直到达边缘?(How to make my borders reach all the way to the edge?)
内联块后防止换行(Prevent line break after inline-block)
CSS背景颜色不能延伸到屏幕边缘(CSS background colour not extending to the edge of the screen)
相关文章
更多Heritrix源码分析(二) 配置文件order.xml介绍
Heritrix源码分析(三) 修改配置文件order.xml加快你的抓取速度
MyBatis动态参数order by的设置
使用shell命令给文件中每一行的前面、后面、指定列添加字符
R简单数据分析
在Hadoop集群上运行R程序--安装RHadoop
请问起点R3能否做到完全支持MultiCore?
nutch与起点R3集成之笔记(一)
nutch与起点R3集成之笔记(二)
谁能指引,谁会指引
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!