0

I am using the separate function in order to deal with a text with variable size.

In the separate function, there is a need to details the name of the new columns in which the text is separate. Nevertheless, in my case the text has variable length. I would like the number of columns to be needed automatically generated.

For instance I am just calculating the number of columns to be needed by counting the number of characters that I am using for key separation (which in my case is ,). Then I enter manually the number of columns to be needed

max(str_count(Applicant_data$Assignee_DWPI, ";"),na.rm = TRUE)

separate(Applicant_data,Assignee_DWPI, c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23"), sep = " ; ")

I would like to get the number of columns automaticly generated.

Cettt
  • 11,460
  • 7
  • 35
  • 58
Plantekös
  • 33
  • 6
  • Tried `ncol()`? – Flash Thunder Apr 29 '19 at 13:12
  • Possible duplicate of [How to use tidyr::separate when the number of needed variables is unknown](https://stackoverflow.com/questions/33288695/how-to-use-tidyrseparate-when-the-number-of-needed-variables-is-unknown) – divibisan Apr 29 '19 at 17:26

1 Answers1

0

almost got it. Try this:

mycols <- max(str_count(Applicant_data$Assignee_DWPI, ";"), na.rm = TRUE)+1

separate(Applicant_data, Assignee_DWPI, as.character(1:mycols), sep = " ; ")

If you not more than 26 columns you can also use

separate(Applicant_data, Assignee_DWPI, letters(1:mycols), sep = " ; ")
Cettt
  • 11,460
  • 7
  • 35
  • 58