Home > Mobile >  Simplify output for combn function to remove spaces and row and column markers
Simplify output for combn function to remove spaces and row and column markers

Time:09-01

I would like to take a data set like the one below generated using the combn function:

https://i.ibb.co/r0qSmYV/example.jpg

and have it print

1111112223
2223343344
3454544555

the current solutions I have tried are trimws() and gsub(). Any help will be greatly appreciated.

CodePudding user response:

You could do:

library(dplyr)
combn(1:5, 3) |> 
  as.data.frame() |>
  mutate(combined = apply(across(everything()), 1, function(x) paste0(x, collapse = ""))) |> 
  pull(combined)

which gives:

[1] "1111112223" "2223343344" "3454554555"

of course, if you don't want to just have a vector and instead have a data frame column, don't run the last line.

CodePudding user response:

libary(tidyverse)
as.data.frame(combn(1:5, 3)) |>  rowwise() |> 
mutate(
  txt = paste0(c_across(),collapse = "")
) |> pull(txt) |> cat(fill=1L)
  • Related