Hi Masters,
I have this simple df:
source <- data.frame(
stringsAsFactors = FALSE,
URN = c("aaa","bbb","ccc",
"ddd","eee","fff","ggg"),
Name = c("xxx","xxx","yyy",
"yyy","yyy","zzzz","abcde"),
Q1 = c("None.",NA,
"No comments related to this exercise","Na",
"N/A","Interesting comment","abc"),
P2 = c("Nothing",
"I have nothing in common","NA",NA,
"Another comment","....?","xxxx"),
Z3 = c("Service","All good",
"aa","I don't know",
"The final comment about that","Nothing.","na"),
Q4 = c(2019,2020,2020,2019,
2020,2021,2021)
)
source
Now, I would like to merge all string variables with a number of characters >5 ("Name" does not meet this criteria as max string length is 5) without specifying their names.
I know I can do it this way:
merged.comments <- source %>%
mutate(all_comments = paste(Q1,P2,Z3, sep="/"))
merged.comments
but I don't want to specify variable names in paste but use mutate_if like:
mutate_if(~is.character(.) & any(nchar(.) > 5, na.rm = TRUE)
How can I do that?