在 cbind 或 rbind 之后丢失表的暗名称
在cbind
或rbind
-ing表对象之后(例如,添加总和的余量或类似的东西),dimnames的名称会丢失(请参阅y
)。我找到了这个“解决方法”,但想知道是否有一个现成的解决方案,看起来不那么老套。也许可以即时完成一些事情?我想保留table
类的对象。
> (x <- table(1:3, sample(1:3), dnn = c("rows", "cols")))
cols
rows 1 2 3
1 1 0 0
2 0 0 1
3 0 1 0
> (y <- cbind(x, "4" = 4:6)) # "rows" and "cols" get lost
1 2 3 4
1 1 0 0 4
2 0 0 1 5
3 0 1 0 6
> names(dimnames(y)) <- names(dimnames(x))
> y
cols
rows 1 2 3 4
1 1 0 0 4
2 0 0 1 5
3 0 1 0 6
After cbind
or rbind
-ing a table object (for example, adding a margin of sums or somesuch), names of dimnames get lost (see y
). I found this "workaround" but was wondering if there's an out of the bag solution to this that looks less hacky. Perhaps something that can be done on the fly? I would like to keep the object of class table
.
> (x <- table(1:3, sample(1:3), dnn = c("rows", "cols")))
cols
rows 1 2 3
1 1 0 0
2 0 0 1
3 0 1 0
> (y <- cbind(x, "4" = 4:6)) # "rows" and "cols" get lost
1 2 3 4
1 1 0 0 4
2 0 0 1 5
3 0 1 0 6
> names(dimnames(y)) <- names(dimnames(x))
> y
cols
rows 1 2 3 4
1 1 0 0 4
2 0 0 1 5
3 0 1 0 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
addmargins
怎么样?它默认计算总和,但您可以插入任何自定义函数。例如:How about
addmargins
? It computes sums by default, but you can plug in any custom function(s). For example: