R:将整个数据帧作为参数传递给自定义函数,还是只需要列?

时间:2017-03-21 03:29:10

标签: r dataframe maintainability

我想知道从可用性/效率的角度来看哪个更好:将数据帧传递给函数,以及所需的列名称,或仅传递数据列。如果必须将结果向量添加到数据集中,是否会有差异?后者将返回一个列表或一个新的数据帧。

这是这样的:

# entire dataframe
f_df <- function(df, col1, col2){
# note: col1 and col2 would be strings
df$sum <- df[,col1] + df[,col2]
df
}
data <- f_df(data, "a","b")

# just the variables
f_col <- function(col1, col2){
col1 + col2
}
data$sum <- f_col(data$a, data$b)

0 个答案:

没有答案