creating a list of character sting and bind with table

I have a database with some text string in one column and i want to make a list of all the string and separated it with comma.

only first comment is binding but i want to bind all the comments with comma separated

df <- data.frame("Dept"=c("OPS", "OPS",  "OPS",   "OPS",    "OPS",   "OPS",    "OPS",    "OPS",   "OPS",   "OPS",   "OPS",    "Sales",    "Sales",    "OPS",   "OPS",   "OPS",   "OPS",   "Sales",    "Sales"),
                 "Team"=c("Team1","Team2","Team3","Team2","Team1","Team2","Team3","Team2","Team1","Team2","Team3","Team2","Team1","Team2","Team1","Team2","Team1","Team2","Team1"),
                 "Sale1"=c(22,4,4,3,5,6,4,5,4,5,5,4,5,4,5,4,4,4,5),
                 "Sale2"=c(22,4,6,3,6,5,4,5,5,4,5,3,4,5,5,1,4,2,5),
                 "Comm"=c(NA,  "Regional is the location", "over all is ok", "Global part is partial",NA,NA, "Regional is the location",NA,   "Global",   "Regional is the location",NA, "Global part is partial",   "Global part is partial",NA, "Global",NA, "Global part is partial",NA,NA))

df1<- df %>% filter(Team == "Team3") 
T1 <- df1 %>% group_by(Dept) %>% summarise(mean=as.numeric(sprintf(round(mean(as.numeric(Sale1),na.rm = TRUE),2),fmt = '%#.1f')))



groupingvar <- "Dept"
com_var <- "Comm"
groupingvar <- rlang::parse_expr(groupingvar)
com_var <- rlang::parse_expr(com_var)

groupingvar <- "Dept"
com_var <- "Comm"
groupingvar <- rlang::parse_expr(groupingvar)
com_var <- rlang::parse_expr(com_var)

  Comments <- dat %>%
         group_by(!!groupingvar)%>% filter(!is.na(!!com_var)) %>% 
         summarise(Texts = gsub(",","",toString(coalesce(!!com_var),collapse = " ")))%>%
         select(-!!groupingvar) %>% unlist() %>% gsub("NA","", .) %>% stringi::stri_trim_both() 

  T1 <- cbind(T1,Comments)


# getting this error
number of rows of result is not a multiple of vector length (arg 2)debug at #68: T1

Can you better describe what the desired output should look like?

I have updated the question

I think you are asking for


T1 <- df1 %>%
  group_by(Dept) %>%
  summarise(mean = round(mean(as.numeric(Sale1), na.rm = TRUE), 1))

groupingvar <- sym("Dept")
com_var <-sym("Comm")

Comments <- df1 %>%
  group_by(!!groupingvar)%>% filter(!is.na(!!com_var)) %>% 
  summarise(Texts = paste0(!!com_var,collapse=","))

T1 <- cbind(T1,Comments)

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.