将数据帧重新排列到表格中,与“融化”相反。
我有这样的巨大数据框:
SN = c(1:100, 1:100, 1:100, 1:100)
class = c(rep("A1", 100), rep("B2", 100), rep("C3", 100), rep("D4", 100)) # total 6000 levels
myvar = rnorm(400)
mydf = data.frame(SN, class, myvar)
我想“解散”到一个表,每个级别作为单列,并且 myvar 填充:
SN A1 B2 C3 D4 .............and so on for all 6000
我怎样才能实现这一点,我知道这是一个简单的问题,但我无法弄清楚。
I have huge dataframe like this:
SN = c(1:100, 1:100, 1:100, 1:100)
class = c(rep("A1", 100), rep("B2", 100), rep("C3", 100), rep("D4", 100)) # total 6000 levels
myvar = rnorm(400)
mydf = data.frame(SN, class, myvar)
I want to "unmelt" to a table with each level as single column and myvar in filled:
SN A1 B2 C3 D4 .............and so on for all 6000
How can I achieve this, I know it is simple question, but I could not figure out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种使用
split
的方法:结果是一个列表,如果组件具有相同的尺寸(在本例中就是这样),则可以轻松地将其转换为
data.frame
:该解决方案的问题在于最终结果的名称需要清理。但是,对于更一般的数据,如果每个类的
SN
不同,这可能会很有用。Another approach with
split
:The result is a list which can be easily converted to a
data.frame
if the components have the same dimensions (which is true in this example):The problem with this solution is that the names of the final result need a cleaning. However, for a more general data, this can be useful if
SN
is different for each class.在基本 R 中,您可以这样做...
或者,使用聚合来获得更简洁的表达式
In base R you could do it like this...
Or, for a more concise expression using aggregate