如何在自定义R函数中包含数据?

lvmkulzt  于 2023-05-26  发布在  其他
关注(0)|答案(2)|浏览(211)

我想在自定义R函数中包含数据参数。现在我正在使用以下函数

Ploy <- function(Sepal.Length = Sepal.Length, Sepal.Width = Sepal.Width, 
                 Petal.Width = Petal.Width, Petal.Length = Petal.Length){
  
  #Calculate some derived parameters
  deltak <- (Sepal.Length - Sepal.Width)/390
  ARk <- Petal.Width*2
  
  dat <- cbind.data.frame(deltak, ARk)
  
  #Fitting quadratic model
  mod <- lm(deltak ~ poly(ARk, 2, raw = TRUE))
  
  deltaK0 <- abs(mod$coefficients[[1]])
  
  Ks <- Petal.Length - deltaK0
  
  return(c(`DeltaK0` = deltaK0, `Ks` = Ks))
}

当我调用函数时,我必须使用iris$,如

Ploy(Sepal.Length = iris$Sepal.Length, Sepal.Width = iris$Sepal.Width, 
     Petal.Width = iris$Petal.Width, Petal.Length = iris$Petal.Length)

我知道有一个使用with的解决方法,如

with(iris, Ploy(Sepal.Length = Sepal.Length, Sepal.Width = Sepal.Width, 
                Petal.Width = Petal.Width, Petal.Length = Petal.Length))

但是我想调用Ploy函数,就像

Ploy(Sepal.Length = Sepal.Length, Sepal.Width = Sepal.Width, 
     Petal.Width = Petal.Width, Petal.Length = Petal.Length, data = iris)

我怎么才能做到呢?

更新

从@user2554330的答案中得到线索,我使用了以下代码

Ploy <- function(data, Sepal.Length = "Sepal.Length", Sepal.Width = "Sepal.Width", 
                 Petal.Width = "Petal.Width", Petal.Length = "Petal.Length"){
  
  Sepal.Length = data[[Sepal.Length]]
  Sepal.Width = data[[Sepal.Width]]
  Petal.Width = data[[Petal.Width]]
  Petal.Length = data[[Petal.Length]]

  #Calculate some derived parameters
  deltak <- (Sepal.Length - Sepal.Width)/390
  ARk <- Petal.Width*2
  
  dat <- cbind.data.frame(deltak, ARk)
  
  #Fitting quadratic model
  mod <- lm(deltak ~ poly(ARk, 2, raw = TRUE))
  
  deltaK0 <- abs(mod$coefficients[[1]])
  
  Ks <- Petal.Length - deltaK0
  
  return(c(`DeltaK0` = deltaK0, `Ks` = Ks))
}

现在该函数可以像这样使用

Ploy(Sepal.Length = "Sepal.Length", Sepal.Width = "Sepal.Width", 
     Petal.Width = "Petal.Width", Petal.Length = "Petal.Length", data = iris)

现在,如果我想对Petal.Length使用一个数值,如

Ploy(Sepal.Length = "Sepal.Length", Sepal.Width = "Sepal.Width", 
     Petal.Width = "Petal.Width", Petal.Length = 55, data = iris)

我应该如何修改代码?我想有一个if-else来检查Petal.Length是否在data中可用,那么前面的函数应该工作,否则它将使用Petal.Length的值。

3qpi33ja

3qpi33ja1#

你要求的是“非标准评价”:你不想用标准的方法计算表达式Sepal.Length
我劝你不要这样做。很难把它做对,这样你就不会在某些特殊情况下有奇怪的行为。tidyverse软件包使用它,即使这些软件包是由非常聪明的人编写的,他们已经非常仔细地考虑过它,它仍然有一些奇怪的bug,例如。处理...参数。
相反,请您的用户使用字符串名称指定列,例如。它们的叫声看起来像

Ploy(col1 = "Sepal.Length", col2 = "Sepal.Width", 
 col3 = "Petal.Width", data = iris)

然后,在函数中,使用完全标准的计算,可以将这些列引用为

data[[col1]]
data[[col2]]
data[[col3]]
stszievb

stszievb2#

您可以根据需要为R函数指定任意多个参数(输入)。您遇到的问题是**,因为您如何调用这些参数**(输入)。
函数语句的示例可能如下所示:

# Function declaration
Ploy <- function(sepal_lenght,sepal_width, petal_witdh, petal_length, data){
   print(paste("sepal_lenght inside function is ", sepal_lenght))
   print(paste("sepal_width inside function is ", sepal_width))
   print(paste("petal_witdh inside function is ", petal_witdh))
   print(paste("petal_length inside function is ", petal_length))
  print(paste("Aditional data inside function is ", data))
}

# Running your function with concrete values
Ploy(10,20,3.4,66, 'complex object')

示例结果将是:

[1] "sepal_lenght inside function is  10"
[1] "sepal_width inside function is  20"
[1] "petal_witdh inside function is  3.4"
[1] "petal_length inside function is  66"
[1] "Aditional data inside function is  complex object"

也就是说,对于您的情况,您可以执行这样的函数:

sepal  <- data.frame(
  "length" = 5.1, 
  "width" = 3.5
)

petal  <- data.frame(
  "length" = 1,4, 
  "width" = 0.2
)

# Function declaration
Ploy <- function(sepal, petal, data){
   print(paste("sepal_lenght inside function is ", sepal$length))
   print(paste("sepal_width inside function is ", sepal$width))
   print(paste("petal_witdh inside function is ", petal$length))
   print(paste("petal_length inside function is ", petal$width))
  print(paste("Aditional data inside function is ", data))
}

# Running your function with concrete values, passing sepal and petal objects
Ploy(sepal, petal, 'more complex object')

因此,您将获得:

[1] "sepal_lenght inside function is  5.1"
[1] "sepal_width inside function is  3.5"
[1] "petal_witdh inside function is  1"
[1] "petal_length inside function is  0.2"
[1] "Aditional data inside function is  more complex object"

相关问题