如何解决用文本参数编写函数
我有一个矩阵(colormatrix),在第一列(称为NCS.code)中具有标识符,在第二列(称为colour.assignment)中具有颜色名称。
我想创建一个函数(changecolor),该函数可以根据标识符名称更改颜色名称(即colour.assignment的值)。我写了以下内容,但不起作用。
changecolor<-function(id,color){
locdat<-match(id,colormatrix$NCS.code)
#finds the location where id matches the identifier
colormatrix_lab$colour.assignment[locdat]<-color}
#changes the color name at location locdat into the name that was given as input
changecolor("S0300-N","white")
#test if the code works -- which is not the case.
感谢所有帮助。
解决方法
要在函数调用中修改全局变量时,不能使用<-
,因为它不会在全局范围内求值。做您想做的-尝试使用:
从函数调用中修改全局数据结构时,<<-
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。