Specific type of variables selected with dplyr and then merged using paste or unite

Hi Masters,
I have this simple df:

source <- data.frame(
  stringsAsFactors = FALSE,
               URN = c("aaa", "bbb", "ccc"),
              Name = c("xxx", "xxx", "yyy"),
              Date = c("2019-04-29", "2019-11-04", "2019-06-18"),
                Q1 = c("None.", NA, "No comments related to this exercise"),
                Q2 = c("Nothing", "I have nothing in common", "NA"),
                Q3 = c("Service", "All good", "aa"),
                Q4 = c(2019, 2020, 2020),
                Q5 = c(10, 9, 8)

Now, I need to select string variables plus some specified variables (in this case just Q5)


selected <- select(source, where(~is.character(.x)), Q5)

Now, I would like to merge all string variables with long comments (longer that 10 characters).
Unfortunately ifendo's solution like this:

merged_comments <- selected %>% 
  unite("all_comments", where(~is.character(.x) && any(nchar(.x) > 10)), sep = "/", remove = FALSE, na.rm = FALSE) 

is not working.
Do you have other options?

Maybe I should select long comments in the first stage by something like this:

selected <- select(source, where(~is.character(.x) && any(nchar(.x) > 10)), Q5, URN)

and then merge all character variables apart from URN?

merged_comments <- selected %>% 
  unite("all_comments", where(~is.character(.x),-URN,-Name), sep = "/", remove = FALSE, na.rm = FALSE) 

Any suggestions?

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.