简体   繁体   English

将列名传递给 R 中的函数

[英]Passing column name to function in R

I am removing NAs from a number of columns of my dataframe.我正在从我的数据帧的许多列中删除 NA。 My dataframe name is pretty long but that is required.我的数据框名称很长,但这是必需的。 It looks a bit messy using the command over and over again so I want to make a function that makes it easier to understand what's happening.一遍又一遍地使用该命令看起来有点混乱,所以我想制作一个函数,使人们更容易理解正在发生的事情。 I did the following:-我做了以下事情:-

ConvNAtoBlank <- function(colname) {
    dataframe[[colname]][is.na(dataframe[[colname]])] = ""
}

This did not work.这没有用。 I have also tried to return the dataframe and assign it again later as:-我还尝试返回数据框并稍后再次将其分配为:-

dataframe <- ConvNAtoBlank("B") # assuming B is a column name

This does not seem to work either.这似乎也不起作用。 Is there anything I'm doing wrong here?我在这里做错了什么吗? I started learning R this Monday so I'm still a newbie and I can't figure this out.我从这个星期一开始学习 R,所以我仍然是一个新手,我无法弄清楚这一点。 Any help would be appreciated.任何帮助,将不胜感激。

You need to return the changed dataframe back at the end of the function.您需要在函数结束时返回更改后的数据帧。 It is a good practice to pass dataframe to the function as well.将数据帧传递给函数也是一种很好的做法。

ConvNAtoBlank <- function(dataframe, colname) {
  dataframe[[colname]][is.na(dataframe[[colname]])] = ""
  dataframe
}

df <- data.frame(A = c('A', NA, 'B'), B = c(NA, NA, 'A'))
ConvNAtoBlank(df, "B")

#     A B
#1    A  
#2 <NA>  
#3    B A

We could use tidyverse methods to pass either quoted/unquoted arguments我们可以使用tidyverse方法来传递带引号/不带引号的参数

library(dplyr)
library(tidyr)
ConvNAtoBlank <- function(dataframe, colname) {
     colname <- rlang::ensym(colname)
     dataframe %>%
       mutate(!! colname := replace_na(!! colname, ""))
   }

-testing -测试

df <- data.frame(A = c('A', NA, 'B'), B = c(NA, NA, 'A'))
ConvNAtoBlank(df, "B")
  A B
1    A  
2 <NA>  
3    B A
ConvNAtoBlank(df,  B)
     A B
1    A  
2 <NA>  
3    B A

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM