将参数传递给用户定义的函数

时间:2022-12-05 19:16:37

I created a user defined function with 3 parameters. While calling the function if I happen to hardcode the value as indicated in the line which is commented everything works well but if I try to make use of parameters I am getting the following error:

我用3个参数创建了一个用户定义的函数。在调用函数的时候,如果我碰巧硬编码了注释行中指示的值,一切运行正常,但如果我尝试使用参数,我会收到以下错误:

Warning message:
In `[<-.data.frame`(`*tmp*`, data$X == "Key1", , value = list(X = integer(0),  :
  provided 17 variables to replace 16 variables

The data frame data contains 16 columns !!!

数据帧数据包含16列!!!

code used :

使用的代码:

Change <- function('Arc', Value, 'Key1'){

  data<-read.csv("matrix.csv")

  #This statement works but the below does not ......   
  #data[data$'X'=='C1',]$'OGB_OGB' <-(data[data$'X'=='C1',]$'OGB_OGB' / Value)  

  data[data$'X'=="Key1",]$"Arc" <-data[data$'X'=="Key1",]$"Arc" / Value     
  return(data)
}

tes<-Change("OGB_OGB",.3,"C1")

I am guessing somewhere i am messing up the strings parameters..please help

我猜某个地方我搞乱了字符串参数..请帮忙

1 个解决方案

#1


1  

You can't define a function

您无法定义功能

  foo <- function('a') {'a'}

This will return the error

这将返回错误

foo <- function('a'

foo < - 功能('a'

So you aren't even creating a function.

所以你甚至没有创建一个函数。

When creating a function using function, you must pass it an list of named arguments,

使用函数创建函数时,必须传递一个命名参数列表,

ie. something like foo <- function(a){} or foo <- function(a = 1){} if you want to give it a "default" value.

即。类似于foo < - function(a){}或foo < - function(a = 1){},如果你想给它一个“默认”值。

Within the function you refer to the arguments using names (symbols not character strings)

在函数中,您使用名称引用参数(符号不是字符串)

You have also got a great example fortune(312)

你也有一个很好的财富榜样(312)

library(fortunes)
fortune(312)

The problem here is that the $ notation is a magical shortcut and like any other magic if used incorrectly is likely to do the programmatic equivalent of turning yourself into a toad. -- Greg Snow (in response to a user that wanted to access a column whose name is stored in y via x$y rather than x[[y]]) R-help (February 2012)

这里的问题是$符号是一个神奇的快捷方式,就像任何其他魔法一样,如果使用不正确,很可能会将程序化等同于将自己变成蟾蜍。 - Greg Snow(响应想要访问名称通过x $ y而不是x [[y]]存储在y中的列的用户)R-help(2012年2月)

Therefore your function could be something like

因此你的功能可能是这样的

Change <- function(Arc,Value, key = 'Key1') {

data<-read.csv("matrix.csv")
# calculate the logical vector only once 
# slightly more efficient
index <- data[['X']]==key
# you might consider  index <- data[['X']] %in% key
# if you wanted more than one value in `key`
# replace as appropriate
data[[Arc]][index] <- data[[Arc]][index] / Value
# return the data
return(data)
}


tes<-Change(Arc = "OGB_OGB",Value = .3,key = "C1")

#1


1  

You can't define a function

您无法定义功能

  foo <- function('a') {'a'}

This will return the error

这将返回错误

foo <- function('a'

foo < - 功能('a'

So you aren't even creating a function.

所以你甚至没有创建一个函数。

When creating a function using function, you must pass it an list of named arguments,

使用函数创建函数时,必须传递一个命名参数列表,

ie. something like foo <- function(a){} or foo <- function(a = 1){} if you want to give it a "default" value.

即。类似于foo < - function(a){}或foo < - function(a = 1){},如果你想给它一个“默认”值。

Within the function you refer to the arguments using names (symbols not character strings)

在函数中,您使用名称引用参数(符号不是字符串)

You have also got a great example fortune(312)

你也有一个很好的财富榜样(312)

library(fortunes)
fortune(312)

The problem here is that the $ notation is a magical shortcut and like any other magic if used incorrectly is likely to do the programmatic equivalent of turning yourself into a toad. -- Greg Snow (in response to a user that wanted to access a column whose name is stored in y via x$y rather than x[[y]]) R-help (February 2012)

这里的问题是$符号是一个神奇的快捷方式,就像任何其他魔法一样,如果使用不正确,很可能会将程序化等同于将自己变成蟾蜍。 - Greg Snow(响应想要访问名称通过x $ y而不是x [[y]]存储在y中的列的用户)R-help(2012年2月)

Therefore your function could be something like

因此你的功能可能是这样的

Change <- function(Arc,Value, key = 'Key1') {

data<-read.csv("matrix.csv")
# calculate the logical vector only once 
# slightly more efficient
index <- data[['X']]==key
# you might consider  index <- data[['X']] %in% key
# if you wanted more than one value in `key`
# replace as appropriate
data[[Arc]][index] <- data[[Arc]][index] / Value
# return the data
return(data)
}


tes<-Change(Arc = "OGB_OGB",Value = .3,key = "C1")