将数据帧重新排列到表格中,与“融化”相反。

发布于 2024-12-11 01:13:19 字数 435 浏览 5 评论 0原文

我有这样的巨大数据框:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

演出会有结束 2024-12-18 01:13:19
> dcast(mydf, SN ~ class)

  SN         A1         B2          C3          D4
1  1  0.1461258  0.8325014  0.33562088 -0.07294576
2  2  0.5964182  0.4593710 -0.23652803 -1.52539568
3  3  2.0247742 -1.1235963  1.79875447 -1.87462227
4  4  0.8184004  1.3486721  0.76076486 -1.18311991
5  5 -0.6577212  0.3666741 -0.06057506  1.38825487
6  6  0.1590443  0.2043661  0.08161778  0.10421797
...
> dcast(mydf, SN ~ class)

  SN         A1         B2          C3          D4
1  1  0.1461258  0.8325014  0.33562088 -0.07294576
2  2  0.5964182  0.4593710 -0.23652803 -1.52539568
3  3  2.0247742 -1.1235963  1.79875447 -1.87462227
4  4  0.8184004  1.3486721  0.76076486 -1.18311991
5  5 -0.6577212  0.3666741 -0.06057506  1.38825487
6  6  0.1590443  0.2043661  0.08161778  0.10421797
...
花开雨落又逢春i 2024-12-18 01:13:19
molten = melt( mydf , id.vars = c( "SN" , "class" ) , measure.vars = "myvar" )
casted = dcast( molten , SN~class )
molten = melt( mydf , id.vars = c( "SN" , "class" ) , measure.vars = "myvar" )
casted = dcast( molten , SN~class )
伴我心暖 2024-12-18 01:13:19

另一种使用 split 的方法:

mydfSplit <- split(mydf[,-2], mydf$class, drop=TRUE)

结果是一个列表,如果组件具有相同的尺寸(在本例中就是这样),则可以轻松地将其转换为 data.frame

mydf2 <- do.call(cbind, mydfSplit)

该解决方案的问题在于最终结果的名称需要清理。但是,对于更一般的数据,如果每个类的 SN 不同,这可能会很有用。

Another approach with split:

mydfSplit <- split(mydf[,-2], mydf$class, drop=TRUE)

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):

mydf2 <- do.call(cbind, mydfSplit)

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.

溺孤伤于心 2024-12-18 01:13:19

在基本 R 中,您可以这样做...

# get it sorted so that all you need to do is make a matrix out of it
mydf <- mydf[order(mydf$class, mydf$SN),]
# save the unique values of SN
SNu <- unique(mydf$SN)
# combine a matrix with SN
mydfw <- data.frame(SNu, matrix(mydf$myvar, nrow = length(SNu)))
# name your columns    
colnames(mydfw) <- c('SN', levels(mydf$class))

或者,使用聚合来获得更简洁的表达式

aggregate(myvar~SN, mydf, 'c')
# column names don't come out great
colnames(mydfw) <- c('SN', levels(mydf$class))

In base R you could do it like this...

# get it sorted so that all you need to do is make a matrix out of it
mydf <- mydf[order(mydf$class, mydf$SN),]
# save the unique values of SN
SNu <- unique(mydf$SN)
# combine a matrix with SN
mydfw <- data.frame(SNu, matrix(mydf$myvar, nrow = length(SNu)))
# name your columns    
colnames(mydfw) <- c('SN', levels(mydf$class))

Or, for a more concise expression using aggregate

aggregate(myvar~SN, mydf, 'c')
# column names don't come out great
colnames(mydfw) <- c('SN', levels(mydf$class))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文