【问题标题】:I give three arguments, the input df, the column I want to clean,the new column I want to be added with cleansed names. Where am I going wrong?我给出了三个参数,输入 df,我想要清理的列,我想要添加清理后的名称的新列。我哪里错了?
【发布时间】:2019-03-22 14:32:08
【问题描述】:
library(dplyr)
clean_name <- function(df,col_name,new_col_name){

 #remove whitespace and common titles.
 df$new_col_name <- mutate_all(df, 
                  trimws(gsub("MR.?|MRS.?|MS.?|MISS.?|MASTER.?","",df$col_name)))

 #remove any chunks of text where a number is present
 df$new_col_name<- transmute_all(df,
                  gsub("[^\\s]*[\\d]+[^\\s]*","",df$col_name,perl = TRUE))

}

我收到以下错误

“错误:列new_col_name 必须是一维原子#vector 或列表”

【问题讨论】:

  • 如果您只传递单列 mutate_alltransmute_all 则不需要。它将是mutate/transmute,在其中,您不需要df$。您是否在传递未引用的参数。在这种情况下,请使用 enquo(colname), enquo(new_colname)`,然后使用 !! 进行评估

标签: r regex gsub


【解决方案1】:

您要做的是确保您使用的函数的输出是一个向量或只有一个维度的列表,以便您可以将其作为新列添加到所需的数据框中。您可以使用基础包中的 Class 函数来验证对象的类。

mutate 函数本身应该做你想做的事,它返回相同的数据框但带有新列:

     library(dplyr)
     clean_name <- function(df, col_name, new_col_name) {

     # first_cleaning_to_colname = The first change you want to make to the col_name column. This should be a vector.
     # second_cleaning_to_colname = The change you're going to make to the col_name column after the first one. This should be a vector too.

     first_change <- mutate(df, col_name = first_cleaning_to_colname)

     second_change <- mutate(first_change, new_col_name = second_cleaning_to_colname)

     return(second_change)
     }

您可以同时进行这两项更改,但我认为这样更容易阅读。

【讨论】:

    【解决方案2】:

    如果我们传递不带引号的列名,则使用

    library(tidyverse)
    clean_name <- function(df,col_name, new_col_name){
       col_name <- enquo(col_name)
      new_col_name <- enquo(new_col_name)
       df %>% 
         mutate(!! new_col_name := 
         trimws(str_replace_all(!!col_name, "MR.?|MRS.?|MS.?|MISS.?|MASTER.?","")) ) %>%
         transmute(!! new_col_name := trimws(str_replace_all(!! new_col_name, 
                  "[^\\s]*[\\d]+[^\\s]*","")))
         }
    
    
    clean_name(dat1, col1, colN) 
    #   colN
    #1  one
    #2  two
    

    数据

    dat1 <- data.frame(col1 = c("MR. one", "MS. two 24"), stringsAsFactors = FALSE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 2021-10-04
      • 2011-01-09
      • 1970-01-01
      • 2022-08-10
      相关资源
      最近更新 更多