我...对一些函数中使用省略号()感到困惑,即如何将包含参数的对象作为单个参数传递.
在Python中,它被称为"解包参数列表",例如
>>> range(3, 6) # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args) # call with arguments unpacked from a list
[3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
例如,在R中你有一个file.path(...)使用省略号的函数.我想有这样的行为:
> args <- c('baz', 'foob')
> file.path('/foo/bar/', args)
[1] 'foo/bar/baz/foob'
Run Code Online (Sandbox Code Playgroud)
相反,我得到了
[1] 'foo/bar/baz' 'foo/bar/foob'
Run Code Online (Sandbox Code Playgroud)
其中的元素args不是"解包"并同时进行评估.是否有R等价于蟒蛇*arg?
我再次对如何实现这一目标感到困惑:
鉴于此数据框:
df <- tibble(
foo = c(1,0,1),
bar = c(1,1,1),
foobar = c(0,1,1)
)
Run Code Online (Sandbox Code Playgroud)
这个向量:
to_sum <- c("foo", "bar")
Run Code Online (Sandbox Code Playgroud)
我想获得列中值的行式总和to_sum。
期望的输出:
# A tibble: 3 x 4
# Rowwise:
foo bar foobar sum
<dbl> <dbl> <dbl> <dbl>
1 1 1 0 2
2 0 1 1 1
3 1 1 1 2
Run Code Online (Sandbox Code Playgroud)
输入它是有效的(显然)。
df %>% rowwise() %>%
mutate(
sum = sum(foo, bar)
)
Run Code Online (Sandbox Code Playgroud)
这不会:
df %>% rowwise() %>%
mutate(
sum = sum(to_sum)
)
Run Code Online (Sandbox Code Playgroud)
我理解,因为如果我要尝试:
df %>% rowwise() …Run Code Online (Sandbox Code Playgroud)