使用data.frame向量的名称作为函数参数的元素

时间:2019-05-21 17:09:47

标签: r

考虑以下示例:

library(tidyverse)

# function my.sum(v)
# arguments:
#  v - numeric vector
# returns:
#  numeric value
#
# accepts the vector as single argument,
# returns sum of the vector elements.
my.sum <- function (v) {
  return(sum(v))
}  

# function my.inverse(val)
# arguments:
#  val - numeric value
# returns:
#  numeric value
#
# changes sign of the argument and returns it
my.inverse <- function (val) {
  return(- val)
}

data.frame(x = rnorm(10, mean = 0, sd = 1), 
           y = rnorm(10, mean = 5, sd = 3)) %>%
  mutate(my_sum = my.sum(c(x,y)),
         my_inverse = my.inverse(x))

执行的结果是:

            x        y   my_sum my_inverse
1  -1.3299817 3.359306 49.23083  1.3299817
2   1.3657651 4.636359 49.23083 -1.3657651
3  -0.2122119 1.760494 49.23083  0.2122119
4   0.7002765 7.396804 49.23083 -0.7002765
5  -0.5828975 4.811493 49.23083  0.5828975
6   1.1202625 4.294421 49.23083 -1.1202625
7   1.2512032 4.907165 49.23083 -1.2512032
8   0.9228939 5.215929 49.23083 -0.9228939
9   0.1800447 0.666941 49.23083 -0.1800447
10 -0.8906996 9.657261 49.23083  0.8906996

正如人们所看到的,my_inverse列是函数my.inverse(val)的返回,它正在从x列中获取值,进行求反并按预期方式写入结果。 / p>

my_sum是函数my.sum(v)的返回,在每一行中都包含常量。此常数是两个整个向量xy的连接元素的总和:

sum(c(x, y))

我希望my.sum()指令内的函数mutate()将接收xy的单个行并将它们连接到c(x, y)中,但是我可以看到c()的行为就像一个闭包。

如何避免这种R行为?

1 个答案:

答案 0 :(得分:1)

使用此替代方法:

set.seed(505)
dat <-data.frame(x = rnorm(10, mean = 0, sd = 1), 
              y = rnorm(10, mean = 5, sd = 3),
              z = rnorm(10, mean = 10, sd = 3)) 

my.sum <- function (df, variables) {
  return(rowSums(df[, variables]))
}  


dat %>%
  mutate(my_sum = my.sum(., c("x","y")))
#            x         y         z    my_sum
# 1  -1.1211894 -1.620691  3.800424 -2.741880
# 2  -1.2820570  4.263010  8.831353  2.980953
# 3  -2.0393425  3.563943 13.118901  1.524600
# 4  -0.9377324  4.397400 11.522940  3.459667
# 5  -0.5101607  6.440323 12.993764  5.930162
# 6  -0.4128447  6.071003 11.765313  5.658158
# 7  -0.9103679  6.300995  8.811942  5.390627
# 8   0.1407611  9.089672 10.621450  9.230433
# 9   0.5647174  7.472852 10.407413  8.037570
# 10 -0.4322744  2.479842 14.400691  2.047568