Home > Blockchain >  length of string across multiple columns, get length difference for each row
length of string across multiple columns, get length difference for each row

Time:11-21

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(., ".")))

How do I calculate the difference between str_length for each row?

Desired output would be: data$str_diff = c(1, 1).

CodePudding user response:

Here you go

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

CodePudding user response:

You can do this:

data$str_diff <- apply(sapply(data, nchar), 1, function(row) diff(range(row)))

Which can be shortened to use only one *apply:

data$str_diff <- apply(data, 1, function(row) diff(range(nchar(row))))

CodePudding user response:

In tidyverse, with pmap:

data %>% 
  mutate(str_diff = pmap_int(across(x:z), ~ diff(range(str_count(c(...))))))

# A tibble: 2 × 4
  x     y     z     str_diff
  <chr> <chr> <chr>    <int>
1 a     aa    a            1
2 b     b     bb           1

CodePudding user response:

a dplyr solution:

data %>% 
  rowwise() %>% 
  mutate(str_diff = diff(range(map(c(x, y, z), str_count))))
  • Related