Excluding some variables from paste or unite

Hi,
I have this simple df containing string variables only:

source <- data.frame(
  stringsAsFactors = FALSE,
  URN = c("aaa","bbb","ccc",
          "ddd","eee","fff","ggg","hhh"),
  Name = c("xxx","xxx","yyy",
           "yyy","yyy","zzzz","abcde","zzzz"),
  A1 = c("None.",NA,
         "No comments related to this exercise","Na",
         "N/A","Interesting comment","abc", "whatever is fine"),
  A2 = c("Nothing",
         "I have nothing in common","NA",NA,
         "Another comment","....?","xxxx", "All fine"),
  B1 = c("Service","All good",
         "aa"," I don't know",
         "The final comment about that","Nothing.","na","Everything"),
  B2 = c("aaa","Nothing ",
         "None","My final comments are ok", "I don't know ",
         "Nothing.","Another comment","really")
)
source

Can I merge only variables containing long phrases [let's say with nchar(.x) > 15)]?
If that is not possible, how can I merge all variables apart from URN and Name?

library(tidyverse)
#choose 
sepchoice <- "_"
#sepchoice <- ""
(united_df <- source %>% 
  mutate(across(.fns=~ifelse(nchar(.)>15 & !is.na(.),.,""))) %>% 
  unite(col = "united",sep=sepchoice))

(result <- bind_cols(source,united_df))

That is useful but I meant overall lengths of the character variables (sorry :unamused: ):
Length of 'URN' is 3,
'Name' is 5 (the longest entry is 'abcde'),
A1 is 36 (the longest entry is 'No comments related to this exercise') ,
A2 is 24 (the longest entry is 'I have nothing in common'),
B1 is 28 (the longest entry is 'The final comment about that') and
B2 is 24 (the longest entry is 'My final comments are ok').

Using this logic, only A1, A2, B1 and B2 should be united.

Is this doable?

library(dplyr)
library(tidyr)

sample_df <- data.frame(
    stringsAsFactors = FALSE,
    URN = c("aaa","bbb","ccc",
            "ddd","eee","fff","ggg","hhh"),
    Name = c("xxx","xxx","yyy",
             "yyy","yyy","zzzz","abcde","zzzz"),
    A1 = c("None.",NA,
           "No comments related to this exercise","Na",
           "N/A","Interesting comment","abc", "whatever is fine"),
    A2 = c("Nothing",
           "I have nothing in common","NA",NA,
           "Another comment","....?","xxxx", "All fine"),
    B1 = c("Service","All good",
           "aa"," I don't know",
           "The final comment about that","Nothing.","na","Everything"),
    B2 = c("aaa","Nothing ",
           "None","My final comments are ok", "I don't know ",
           "Nothing.","Another comment","really")
)

sample_df %>%
    unite("united", where(~any(nchar(.) > 15)), sep = " ", remove = FALSE) %>% 
    as_tibble()
#> # A tibble: 8 × 7
#>   URN   Name  united               A1            A2         B1         B2       
#>   <chr> <chr> <chr>                <chr>         <chr>      <chr>      <chr>    
#> 1 aaa   xxx   "None. Nothing Serv… None.         Nothing    "Service"  "aaa"    
#> 2 bbb   xxx   "NA I have nothing … <NA>          I have no… "All good" "Nothing…
#> 3 ccc   yyy   "No comments relate… No comments … NA         "aa"       "None"   
#> 4 ddd   yyy   "Na NA  I don't kno… Na            <NA>       " I don't… "My fina…
#> 5 eee   yyy   "N/A Another commen… N/A           Another c… "The fina… "I don't…
#> 6 fff   zzzz  "Interesting commen… Interesting … ....?      "Nothing." "Nothing…
#> 7 ggg   abcde "abc xxxx na Anothe… abc           xxxx       "na"       "Another…
#> 8 hhh   zzzz  "whatever is fine A… whatever is … All fine   "Everythi… "really"

Created on 2021-12-03 by the reprex package (v2.0.1)

Thank you.
I also want to save another df just with the long string variables (>25 characters) plus URN.
Can I simply modify this?

sample_df.str <- select(sample_df, URN, where(~any(nchar(.) > 25))

Can you help?

Thank you! Can you also please advise how I could:

  1. unite all string variables in the df without URN (without specifying the length)?
  2. unite all variables in the df without URN ?

Thank you

You can learn how to do all of that by reading the tidyselect documentation

Thank you. I've gone through this already and I wouldn't bother you, experienced R masters, with something very simple (I always try to find a solution myself first). It's about combinations of commands and exclusions which I cannot find...

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.