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.
相关问答
更多-
我一直在寻找这个已经很长一段时间试图弄清楚发生了什么,因为这对我来说非常有趣。 这就是我的结论: 事实上,正如你所看到的那样,文本正在向边缘运行,但是通过调用inline它只是表示容器应该更小的错觉,因为它强制将consectetr这个词强加到下一行。 我注意到的一些事情(我删除了你的内联CSS并将其添加到JsFiddle的CSS部分以获得更好的可视性): 原始代码: 原始的FIDDLE 1.将p设置为inline-block或inline并添加分word-break: break-all您可以看到cons ...
-
发生换行是因为内联块不能像多个普通的内联元素一样分割成多行。 它只是一个完整的“块单位”,它以内联方式显示。 如果整个单元不合适,那么它将全部包装到下一行。 The line break happens because the the inline block cannot be split across multiple lines like a normal inline element. It is simply one entire "block unit" that is displayed in ...
-
.white-space: nowrap
- 上的.white-space: nowrap 。 不要浮动元素,但使用display: inline-block 。 http://jsfiddle.net/nJydR/3/ .white-space: nowrap on the
- . Do not float the elements, but use display: inline-block. http://jsfiddle.net/nJydR/3/
-
在第二个容器周围使用另一个包装器DIV ......
Normal Boxed Width
看来我找到了解决方案。 .post img{ display:inline-block; vertical-align: top; } .post_content{ display:inline-block; width: 90%; } 我的代码笔 : 代码笔 It appears I have found the solution. .post img{ display:inline-block; vertical-align: top; } .post_content{ ...将背景延伸到边距(Extend a background into the margin)[2022-07-08]
切换margin: 10px到padding: 10px ,它应该做的伎俩。 Switch margin: 10px to padding: 10px and it should do the trick.内联换行符?(Inline line breaks?)[2023-06-21]
这似乎提供了一个内联中断的解决方案: http : //codepen.io/pageaffairs/pen/oliyz