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 using cast. 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
2022-09-07 21:09

满意答案

我一直在寻找这个已经很长一段时间试图弄清楚发生了什么,因为这对我来说非常有趣。 这就是我的结论:

事实上,正如你所看到的那样,文本正在向边缘运行,但是通过调用inline它只是表示容器应该更小的错觉,因为它强制将consectetr这个词强加到下一行。 我注意到的一些事情(我删除了你的内联CSS并将其添加到JsFiddle的CSS部分以获得更好的可视性):

原始代码: 原始的FIDDLE

1.p设置为inline-blockinline并添加分word-break: break-all您可以看到consectetr这个词完全适合框中,正好在边缘上方:

FIDDLE 1 - 内联块

FIDDLE 2 - 内联

2.如果您使用原始代码并将p s父div更改为inline而不是inline-block ,则可以看到文本周围有红色边框,但它不会包装容器,它会包装文本字符串,甚至打破句子的作用:

FIDDLE 3 - 父内联

3.最后,如果您要添加换行符,结果将如您所愿:

FIDDLE 4 - 换行

所以我的最后结论是,虽然看起来字符串被分成两条不同的线并且小于它的父宽度,但这只是一种幻觉,它实际上仍然占据了它的父级的全宽,除非一个标签专门告诉它在哪里打破阵容。


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 word consectetr 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 to inline-block or inline and adding word-break: break-all you can see the word consectetr perfectly fits into the box, right above the edge:

FIDDLE 1 - inline-block

FIDDLE 2 - inline

2. If you use your original code and change the ps parent div to inline instead of inline-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:

FIDDLE 3 - parent inline

3. And finally if you were to add in a line-break, the results would be as you expected:

FIDDLE 4 - line break

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它只是表示容器应该更小的错觉,因为它强制将consectetr这个词强加到下一行。 我注意到的一些事情(我删除了你的内联CSS并将其添加到JsFiddle的CSS部分以获得更好的可视性): 原始代码: 原始的FIDDLE 1.将p设置为inline-block或inline并添加分word-break: break-all您可以看...

就换行符而言,内联块与内联元素(Inline-block vs inline elements in terms of line-breaks)

发生换行是因为内联块不能像多个普通的内联元素一样分割成多行。 它只是一个完整的“块单位”,它以内联方式显示。 如果整个单元不合适,那么它将全部包装到下一行。 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 display...

如何使用float而不是换行符来扩展元素并将其扩展到父容器之外?(How do I make elements with float not line break and extend outside their parent container?)

.white-space: nowrap <ul>上的.white-space: nowrap 。 不要浮动元素,但使用display: inline-block 。 http://jsfiddle.net/nJydR/3/ .white-space: nowrap on the <ul>. Do not float the elements, but use display: inline-block. http://jsfiddle.net/nJydR/3/

获取具有不同背景颜色的两列,这些颜色延伸到屏幕边缘[重复](Get Two Columns with different background colours that extend to screen edge [duplicate])

在第二个容器周围使用另一个包装器DIV ...... <div class="container" style="background: bisque;"> <div class="row"> <div class="col-xs-12"> <h1>Normal Boxed Width</h1> </div> </div> </div> <div style="background-color: aquamarine; pad...

带换行符的内联段落元素不会保持内联(inline paragraph element with line break does not remain inline)

看来我找到了解决方案。 .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_conte...

将背景延伸到边距(Extend a background into the margin)

切换margin: 10px到padding: 10px ,它应该做的伎俩。 Switch margin: 10px to padding: 10px and it should do the trick.

内联换行符?(Inline line breaks?)

这似乎提供了一个内联中断的解决方案: http : //codepen.io/pageaffairs/pen/oliyz <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <style media="all"> [data]:after { content: attr(data); white-space: pre; height: 0; } .doublespace{...

如何使我的边界一直到达边缘?(How to make my borders reach all the way to the edge?)

你意识到<table>意味着包含什么吗? 表格布局用于表格数据。 您不在Excel中写邮件。 本文可能会帮助您做出正确的决定。 设置border-spacing:30px; 到<table> ,你无法实现它。 使用<div>会很容易。 您可以通过以下方式重建您的布局: http : //jsfiddle.net/Qtmkw/4/ ,因此您有两个<table>而不是一个,这可行:) Do you realise what <table> is meant to contain? Table layo...

内联块后防止换行(Prevent line break after inline-block)

看起来第一个字符不需要在<nobr>元素中,所以这将起作用: <nobr><span class="inlineblock"></span></nobr>wide... 仍然很难看,但绝对不那么难看! 它至少适用于Firefox和Chrome。 It looks like the first character doesn't need to be in the <nobr> element, so this will work: <nobr><span class="inlineblock">...

CSS背景颜色不能延伸到屏幕边缘(CSS background colour not extending to the edge of the screen)

确保身体边距设置为0。 body { margin: 0; } Make sure the margin from the body is set to 0. body { margin: 0; }

相关文章

更多

Heritrix源码分析(二) 配置文件order.xml介绍

本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.javaeye.co ...

Heritrix源码分析(三) 修改配置文件order.xml加快你的抓取速度

本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.javaeye.co ...

MyBatis动态参数order by的设置

mybatis动态参数一般是这样的 and status=# {status} 如果你在order b ...

使用shell命令给文件中每一行的前面、后面、指定列添加字符

shell给一个文件中的每一行开头插入字符的方法:awk '{print &quot;X&quot;$ ...

R简单数据分析

眼下大数据口号满天飞,今天拿我微信圈朋友一段时间内分享内容作为数据,用R包的算法实现简单分析。 由于微 ...

在Hadoop集群上运行R程序--安装RHadoop

Hadoop是由Revolution Analytics发起的一个开源项目,它可以将统计语言R与Had ...

请问起点R3能否做到完全支持MultiCore?

在RivuSchema类中实现了从数据库获取数据类型和索引结构表中的数据构建Schema.xml,但这 ...

nutch与起点R3集成之笔记(一)

百度、google帮我们找Internet的信息,但对于一个行业内部网(intranet)来说,百度、 ...

nutch与起点R3集成之笔记(二)

在nutch与起点R3集成之笔记(一)中介绍了在起点R3中添加nutch要用到的索引字段,上述字段建好 ...

谁能指引,谁会指引

最近找了个实习,在一线城市,小公司,做外包 ,j2ee的,每天朝九晚九 做了两个星期,做前台,用sw ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本