请帮忙实现R 语言的多态。

g
gooog
楼主 (北美华人网)
#############first #input: Z foo <- function(Z) { x <- Z[,1] y <- Z[,-1] #do calculation with x and y ... }
##############second
x <- Z[,1] y <- Z[,-1] #input: x and y foo <- function(x,y) { #do calculation with x and y ... }
请问如何在R里面编写一个函数,实现上面两个函数的功能呢?
被逼成了怨妇
没看懂,1, 2 不是一模一样? 一般不建议input是 data frame ,第二个比较好。
library(tidyverse)
x = Z$col1 y = Z %>% select( LastCol) # if Z[,-1] is python pandas (last column) . y = Z %>% select( -LastCol) # if Z[,-1] means all columns except last columns res = foo (x,y)
一定要放一起,
foo = function (df, x ,y) { # df is data frame # x , y are string (column names)
x'' = df %>% select (c(x)) y''= df %>% select (c(y)) .... }