将用户定义的函数应用于小标题

时间:2019-06-27 22:35:29

标签: r function mutate

我创建了一个用户定义的函数,该函数将在文本中搜索某些值,然后返回不同的值。这对于每个单独的调用都有效,但是,当我尝试在Tidyverse中使用它时,使用mutate不再起作用。我收到警告:

警告消息:

  

在if(grepl(“ Unique”,textValue)){中:条件的长度> 1   并且只会使用第一个元素

我猜想它与类型和格式有关,但不确定如何解决。

# create fake data
P1 = c("Unique Claims", "Unique Records", "Spend Today", "Spend Yesterday", "% Returned", "% Claimed")
P2 = as.tibble(P1) 


#create function
assignFormat <- function (textValue = as.character()) {
  if (grepl("Unique", textValue) > 0) {
    numFormat = "Comma"
  } else if (grepl("Spend", textValue) > 0) {
    numFormat = "Currency"
  } else if (grepl("%", textValue, ) > 0 ) {numFormat = "Percent"}
    else numFormat = "Other"

  return(numFormat)
}


#test function - works fine
assignFormat("% of CLaims")
assignFormat("Unique Records")
assignFormat("Total Spend")

#doesn't work
P3 = P2 %>%
     mutate(y = assignFormat(value))

我尝试过的事情: 切换到grep 直接在突变中使用GREP-而是创建三个向量

感谢您的选择和帮助!

3 个答案:

答案 0 :(得分:2)

如果您使用dplyr分组,许多字符串函数可以按rowwise的预期工作

#does work
P3 = P2 %>%
  rowwise() %>% 
  mutate(y = assignFormat(value)) %>% 
  ungroup()

答案 1 :(得分:2)

要使用相同的功能,可以使用map变体

library(dplyr)
library(purrr)

P2 %>%  mutate(y = map_chr(value, assignFormat))

# A tibble: 6 x 2
#  value            y       
#  <chr>           <chr>   
#1 Unique Claims   Comma   
#2 Unique Records  Comma   
#3 Spend Today     Currency
#4 Spend Yesterday Currency
#5 % Returned      Percent 
#6 % Claimed       Percent 

您还可以更改功能以使用ifelse代替if

assignFormat <- function (textValue = as.character()) {
   ifelse(grepl("Unique", textValue), "Comma", 
          ifelse(grepl("Spend", textValue), "Currency", 
              ifelse(grepl("%", textValue),"Percent", "Other")))
}

P2 %>% mutate(y = assignFormat(value))

或者另一种选择是使用专为此类操作设计的case_when

P2 %>%
  mutate(y = case_when(grepl("Unique", value) ~ "Comma", 
                       grepl("Spend", value) ~ "Currency", 
                       grepl("%", value) ~ "Percent", 
                       TRUE ~ "Other"))

答案 2 :(得分:1)

使用private async void _Loaded(object sender, RoutedEventArgs e) { var n = await InitializeLabTests; allTests = new ObservableCollection<CommonProcedure>(n); pData = new ObservableCollection<CommonProcedure>(n); } //ItemsSource - pData //There is a string attribute - wTitle included in the fooClass (DisplayMemberPath) private ObservableCollection<CommonProcedure> __pData; public ObservableCollection<CommonProcedure> pData { get { return __pData; } set { __pData = value; RaisePropertyChanged(); } } private string _SearchText; public string SearchText { get { return _SearchText; } set { _SearchText = value; RaisePropertyChanged(); //Update your ItemsSource here with Linq pData = new ObservableCollection<CommonProcedure> ( allTests.Where(q => q.description.Contains(SearchText)) ); } }

sapply

要添加到数据框中:

> sapply(P2$value, assignFormat)
  Unique Claims  Unique Records     Spend Today Spend Yesterday      % Returned       % Claimed 
        "Comma"         "Comma"      "Currency"      "Currency"       "Percent"       "Percent" 

该错误消息实际上是有益的。该函数被设计为在单个元素上工作,因此我们使用P2 %>% mutate(y = sapply(value, assignFormat)) # A tibble: 6 x 2 value y <chr> <chr> 1 Unique Claims Comma 2 Unique Records Comma 3 Spend Today Currency 4 Spend Yesterday Currency 5 % Returned Percent 6 % Claimed Percent 系列函数对其进行“向量化”。由于我们希望每个输入只有一个结果,因此我们使用apply返回一个输出向量。