[英]Mutate data frame via function in R
Sorry for the basic question, but I could not find an example in this forum to solve this question. 对不起这个基本问题,但是我找不到这个论坛中的示例来解决这个问题。 I've tried this one and this one .
我已经试过这一个 ,这一个 。
I want to change / create a new variable in my data.frame via function in R and tidyverse: 我想通过R和tidyverse中的函数在data.frame中更改/ 创建一个新变量 :
Example: 例:
crivo <- function(x) {
x <<- x %>%
mutate(resp_1 = if_else(MEMO_RE01 == 0,"VN","FP")) %>%
mutate(resp_2 = if_else(MEMO_RE02 == 1,"VP","FN"))
}
crivo(memo_re)
My data.frame name is "memo_re" , but I'll use this function to other datasets as well, just by changing the x argument. 我的data.frame名称是“ memo_re” ,但是我也将通过更改x参数将此函数也用于其他数据集。 R is creating a new data.frame named x instead of creating a new variable in "memor_re" (original dataset).
R正在创建一个名为x的新data.frame,而不是在“ memor_re”(原始数据集)中创建一个新变量。 In other words, I want to assign a function to do that:
换句话说,我想分配一个函数来做到这一点:
memo_re <- memo_re %>% mutate(resp_1 = if_else(MEMO_RE01 == 0,"VN","FP"))
But I need to change many datasets and because of that, I want to be able to specify which dataset I'll change. 但是我需要更改许多数据集,因此,我希望能够指定要更改的数据集。
reproducible code 可复制的代码
library(tidyverse)
memo_re <- data.frame(MEMO_RE01=rep(c(0,1),100), MEMO_RE02=c(0,1))
crivo <- function(x) {
x <<- x %>%
mutate(resp_1 = if_else(MEMO_RE01 == 0,"VN","FP")) %>%
mutate(resp_2 = if_else(MEMO_RE02 == 1,"VP","FN"))
}
crivo(memo_re)
R
is doing exactly what you've asked it to do. R
确实在执行您要求的操作。 In your crivo
function definition, you've written your function to assign the new data frame you've created called x
to the R
environment. 在您的
crivo
函数定义中,您已经编写了将要创建的新数据框x
分配给R
环境的x
。 That's what the <<-
operator does. 这就是
<<-
运算符的作用。 After running your code, use ls()
to see what's in your environment, then look at x
. 运行代码后,使用
ls()
查看环境中的内容,然后查看x
。 You'll see everything is there, just as you've asked it to be, including the correctly mutate x
dataframe. 正如您所要求的那样,您将看到所有内容,包括正确变异的
x
数据框。
> memo_re <- data.frame(MEMO_RE01=rep(c(0,1),100), MEMO_RE02=c(0,1))
>
> crivo <- function(x) {
+ x <<- x %>%
+ mutate(resp_1 = if_else(MEMO_RE01 == 0,"VN","FP")) %>%
+ mutate(resp_2 = if_else(MEMO_RE02 == 1,"VP","FN"))
+ }
> crivo(memo_re)
> ls()
[1] "crivo" "memo_re" "x"
> head(x)
MEMO_RE01 MEMO_RE02 resp_1 resp_2
1 0 0 VN FN
2 1 1 FP VP
3 0 0 VN FN
4 1 1 FP VP
5 0 0 VN FN
6 1 1 FP VP
Now, if you wanted to have crivo()
return something that you could then assign any name you wanted, you should use 现在,如果您想让
crivo()
返回某些内容,然后可以分配您想要的任何名称,则应该使用
crivo <- function(x) {
x %>%
mutate(resp_1 = if_else(MEMO_RE01 == 0,"VN","FP"),
resp_2 = if_else(MEMO_RE02 == 1,"VP","FN"))
}
Note that I haven't used the <<-
operator anywhere. 请注意,我在任何地方都没有使用
<<-
运算符。 As a result, the crivo
fx will be returning the mutated x
dataframe so that you could do 结果,
crivo
fx将返回变异的x
数据帧,以便您可以
new <- memo_re %>% crivo()
This way, you can pipe anything you want to crivo
and assign it to any new variable. 这样,您就可以通过管道
crivo
任何想要crivo
并将其分配给任何新变量。 Alternatively, if you just wanted to call the function on memo_re
, you can do that too: 另外,如果您只想在
memo_re
上调用该函数,也可以这样做:
memo_re <- memo_re %>% crivo()
Note that the "classic" way to write a function is to use return()
to specify what you want a fx to return. 请注意,编写函数的“经典”方法是使用
return()
来指定您希望fx返回的内容。 If you don't use return()
(as I haven't above), R
will return whatever is in the last line. 如果您不使用
return()
(如上所述), R
将返回最后一行中的任何内容。 Here, it's just the mutate dataframe. 在这里,这只是变异数据框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.