我正在尝试使用substitute
函数,但是不确定nchar(substitute(a + b + c))
的结果是什么?答案是
nchar(substitute(a + b + c))
[1] 1 5 1
它到底算什么?我认为中间项只是该表达式的元素数量。但那么以下内容算什么,其他1代表什么?
nchar(substitute(a + b))
[1] 1 1 1
我正在尝试使用substitute
函数,但是不确定nchar(substitute(a + b + c))
的结果是什么?答案是
nchar(substitute(a + b + c))
[1] 1 5 1
它到底算什么?我认为中间项只是该表达式的元素数量。但那么以下内容算什么,其他1代表什么?
nchar(substitute(a + b))
[1] 1 1 1
v1 <- as.character(substitute(a + b + c))
v1
#[1] "+" "a + b" "c"
nchar(v1)
#[1] 1 5 1
计算空格也会给出字符数。
而
as.character(substitute(a + b))
#[1] "+" "a" "b"
as.character(substitute(a + b))
是因为a + b
基本上是+(a, b)
,但是为什么还要另外分割成[1] "+" "a + b" "c"
? - Manuel R"+", "a", "b"
但是当超过2个术语时,第二个术语应该有剩余的+
。你可以检查as.character(substitute(a + b + c + d))
。我不知道为什么会被分成那样。 - akrun+
是一个从左到右的运算符。所以a + b + c
相当于"+"(a + b, c)
。 - Rich Scriven