【问题标题】:length of string across multiple columns, get length difference for each row跨多列的字符串长度,获取每行的长度差
【发布时间】:2022-11-21 20:43:53
【问题描述】:
data = tibble(x = c("a", "b"), y = c("aa", "b"), z = c("a", "bb"))

data %>% 
  mutate(str_length = across(c(x, y, z), ~ str_count(., ".")))

如何计算每一行的 str_length 之间的差异?

期望的输出将是:data$str_diff = c(1, 1)。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    干得好

    data %>% 
      mutate(
        str_diff = abs(str_count(x) - str_count(y))
      )
    
     x     y         z str_diff
      <chr> <chr> <dbl>    <int>
    1 a     aa        1        1
    2 b     b         2        0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多