Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使函数在R中赋值运算符的LHS上工作_R - Fatal编程技术网

使函数在R中赋值运算符的LHS上工作

使函数在R中赋值运算符的LHS上工作,r,R,看来我应该知道答案,但我不知道。如何编写在赋值运算符左侧工作的函数?例如,在下面的示例中,如何创建一个名为my.rownames的函数,根据?rownames row.names返回一个字符向量 row.names还有一个用于rownamesrownames的赋值函数。我想我需要进一步思考我的问题。实际上我对行名并不特别感兴趣。是什么使函数在LHS上工作?@user111024这是因为它是不同的赋值函数。如果您输入了有意义的行名称。谢谢 # get rownames and change the

看来我应该知道答案,但我不知道。如何编写在赋值运算符左侧工作的函数?例如,在下面的示例中,如何创建一个名为
my.rownames
的函数,根据
?rownames

row.names返回一个字符向量


row.names还有一个用于
rownames
rownames的赋值函数。我想我需要进一步思考我的问题。实际上我对行名并不特别感兴趣。是什么使函数在LHS上工作?@user111024这是因为它是不同的赋值函数。如果您输入了有意义的
行名称。谢谢
# get rownames and change them
foo <- rownames(mtcars)
foo <- paste("x",foo)
# put altered rownames back
rownames(mtcars) <- foo
# create a new function my.rownames
my.rownames <- rownames
# works
my.rownames(mtcars)
# doesn't work
my.rownames(mtcars) <- foo
`my.rownames<-` <- `rownames<-`
data(mtcars)
my.rownames(mtcars) <- foo
head(mtcars)
#                     mpg cyl disp  hp drat    wt  qsec vs am gear carb
#x Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#x Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#x Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#x Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#x Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#x Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1