R中如何将列转换为行?
我也有同样的问题。我有这种顺序的数据: ;=column
D1 ;hurs
1 ;0.12
1 ;0.23
1 ;0.34
1 ;0.01
2 ;0.24
2 ;0.67
2 ;0.78
2 ;0.98
我喜欢这样:
D1; X; X; X; X
1;0.12; 0.23; 0.34; 0.01;
2;0.24; 0.67; 0.78; 0.98;
我想根据 D1 对它进行排序并想重塑它?有人有想法吗?我需要对 D1 的 7603 个值执行此操作。
I kind of have the same problem. I have data in this kind of order: ;=column
D1 ;hurs
1 ;0.12
1 ;0.23
1 ;0.34
1 ;0.01
2 ;0.24
2 ;0.67
2 ;0.78
2 ;0.98
and I like to have it like this:
D1; X; X; X; X
1;0.12; 0.23; 0.34; 0.01;
2;0.24; 0.67; 0.78; 0.98;
I would like to sort it with respect to D1 and like to reshape it? Does anyone have an idea? I need to do this for 7603 values of D1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会研究 Hadley 的
reshape
包。它可以做各种很棒的事情。下面的代码将适用于您的玩具示例,但可能有一种更优雅的方法来执行此操作。简而言之,您的数据已经呈现为?melt
形式,因此您可以简单地?cast
它。另外,请查看这些链接
http://www.statmethods.net/management/reshape.html
http://had.co.nz/reshape/
I would look into Hadley's
reshape
package. It does all sorts of great stuff. The code below will work with your toy example, but there is probably a more elegant way of doing this. Simply, your data already appear to be in the?melt
form, so you can simply?cast
it.Also, check out these links
http://www.statmethods.net/management/reshape.html
http://had.co.nz/reshape/
挖掘不可能被认领的骷髅,为什么不使用aggregate()呢?
如果 D1 中每个 id 的长度不相同,您还可以在首先创建“时间”变量后使用基本 R
reshape()
:Digging up skeletons not likely to ever be claimed, why not use
aggregate()
?If the lengths of each id in D1 are not the same, you can also use base R
reshape()
after first creating a "time" variable:我假设每个 D1 的小时数不相等(7603 值)
I have assumed that there are unequal number of hurs per D1 (7603 values)
reshape2 实际上比 reshape 更好。使用 reshape 比 reshape2 使用更多的内存和时间(至少对于我使用 900 万行之类的特定示例而言)。
reshape2 is actually better than reshape. Using reshape uses significantly more memory and time than reshape2 (at least for my specific example using something like 9million rows).
您可以检查 Hadley Wickham 的 reshape 包及其cast() 函数
http://had.co.nz/reshape/< /a>
You might check Hadley Wickham's reshape package and its cast() function
http://had.co.nz/reshape/