简体   繁体   English

如何在R中反映的函数中进行长参数

[英]How to make long argument in function reflected in R

Thanks for all your helps.感谢您的所有帮助。

I want to make macro with function with R. In function , there is long argument.我想用 R 制作带有function宏。在function ,有很长的参数。 This argument will not print or return specific value.此参数不会printreturn特定值。 But I want to make the results of arguments be reflected.但我想让争论的结果得到反映。

For example, I made some arguments with dplyr例如,我与dplyr进行了一些争论

The i will be dataframe. i将是数据框。 I created the dataset test我创建了数据集test

set.seed(1001)
a<-rnorm(10)
b<-rnorm(10)
test<-data.frame(cbind(a,b))
test

Then I want to make column c .然后我想制作列c The c will be a minus b c将是ab

fun<-function(i){
i<-i%>%mutate(c=a-b)}
fun(i=test)

But, when i checked the test , the c variable was not generated.但是,当我检查test ,没有生成c变量。

How to make the results of arguments in function reflected?如何让函数中参数的结果体现出来?

You need to add return in the function and assign the value back to test .您需要在函数中添加return并将值分配回test Or since R by default returns the last line of the function, you can do或者因为 R 默认返回函数的最后一行,你可以这样做

library(dplyr)

fun <- function(i) i %>% mutate(c = a - b)
test <- fun(test)
test

#        a      b      c
#1   2.189  0.303  1.886
#2  -0.178  1.634 -1.812
#3  -0.185 -0.622  0.437
#4  -2.507  0.467 -2.974
#5  -0.557  1.419 -1.977
#6  -0.144  0.110 -0.254
#7   1.092  1.870 -0.778
#8  -0.623 -1.030  0.407
#9  -0.907 -1.342  0.434
#10 -1.594  0.554 -2.148

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使概率分布成为R中函数的参数? - How to make probability distribution an argument of a function in R? 如何使函数参数成为R中向量的元素? - How to make function argument an element from the vector in R? 如何制作带有FASTA格式参数的R函数 - How to make a R function which has an argument in FASTA format 如何在我想用来进行连接的 R function 中传递参数 - How to pass an argument in a R function that I want to use to make a join 在R中创建一个将列表作为参数的函数 - Make a function in R that takes a list as an argument R:将符号向量传递给函数而不是长参数列表 - R: pass a vector of symbols into a function instead of a long argument list 如何使用 R 中的 function 中的参数名称来创建列名? - How can I make column name using argument name in a function in R? R:在使用 dplyr 的 function 中,如何检查以确保在继续之前未引用参数名称? - R: In a function using dplyr, how do I check to make sure that an argument name is not quoted before proceeding? R 3.1.0如何使用1个参数使函数在2列之间选择(子集或索引)? - R 3.1.0 How to make a function to select between 2 columns (subsetting or indexing) using 1 argument? 如何在R中“替换”(或等效功能)以获得较长列表 - how to `replace` (or equivalent function) in R for long lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM