网格单位向量中的元素替换 [英] element replacement in grid unit vector

查看:92
本文介绍了网格单位向量中的元素替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对此感到困惑不解了,所以对于那些可能偶然遇到相同问题的人来说,这是一个虚假的问题.

I've been baffled by this a few times already, so here's a made-up question for others who might stumble upon the same problem.

考虑此网格单位矢量,

a = unit(1:3, c("cm", "in", "npc"))

我想用新值替换一些元素.自然的方法是,

I want to replace some elements with new values. The natural approach would be,

a[1] = unit(2,"pt")
a
# [1] 2cm  2in  3npc

出了点问题:仅更改了数字值,未更改单位.为什么?该怎么办?

Something went wrong: only the numeric value was changed, not the unit. Why? What to do?

如下面的一个答案所指出的那样,此类单位只是具有属性的数字向量.但是,它们的后代unit.arithmeticunit.list也应被视为完全通用的解决方案(例如,用于调整ggplot对象的面板大小).考虑这个单位向量,

As pointed out in one answer below, such units are just numeric vectors with attributes. However, their offsprings unit.arithmetic and unit.list should also be considered solution to be fully general (e.g to use in adjusting panel sizes of ggplot objects). Consider this unit vector,

(b = a + unit(1, "npc"))
# [1] 1cm+1npc  2in+1npc  3npc+1npc
# [1] "unit.arithmetic" "unit"   

现在替换特定的元素变得更加棘手,因为它们不再是原子性的.

Now replacing a specific element is more tricky, since they're not atomic anymore.

推荐答案

grid 现在有[<-.unit方法.

这篇关于网格单位向量中的元素替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆