Separate a String using Tidyr's “separate” into Multiple Columns and then Create a New Column w


So I have the basic dataframe below which contains long strings separated by a comma.I used Tidyr's "separate" to create new columns.

How do I add another new column with counts of how many new columns there are for each person that contain an answer? (no NA's).

I suppose the columns can be counted after being separated, or before, by counting how many string elements there are that are separated by a comma?

Any help would be appreciated. I would like to stay within the Tidyverse and dplyr.

Name<-c("John","Chris","Andy") Goal<-c("Go back to school,Learn to drive,Learn to cook","Go back to school,Get a job,Learn a new Skill,Learn to cook","Learn to drive,Learn to Cook") df<-data_frame(Name,Goal) df<-df%>%separate(Goal,c("Goal1","Goal2","Goal3","Goal4"),sep=",")


We can try with str_count

library(stringr) df %>% separate(Goal,paste0("Goal", 1:4), sep=",", remove=FALSE) %>% mutate(Count = str_count(Goal, ",")+1) %>% select(-Goal) # Name Goal1 Goal2 Goal3 Goal4 Count # <chr> <chr> <chr> <chr> <chr> <dbl> #1 John Go back to school Learn to drive Learn to cook <NA> 3 #2 Chris Go back to school Get a job Learn a new Skill Learn to cook 4 #3 Andy Learn to drive Learn to Cook <NA> <NA> 2


  • MongoDB Count() vs. Aggregation
  • BigQuery : is it possible to execute another query inside an UDF?
  • cannot run python script file using windows prompt
  • How can I convert this tuple of tuples into a count of its elements?
  • Python : number of characters in text file
  • Group By and add columns
  • Pivot table calculated Field from count of values
  • Ruby: Invert a hash to also preserve non unique values
  • leaflet marker not displaying in certain contexts
  • How to retrieve a specific string from a specific list from a file with JSON in Python
  • Hiding Directories Programatically in C#
  • Android Chronometer starts and stops but carries on counting when stopped
  • Haskell: function composition with anonymous/lambda function
  • pandas computation in each group
  • Creating a Multidimensional, Associative Array in VBScript
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • Plotting line graph with factors in R
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • How VBA declared Volatility works
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Caching attributes in superclass
  • Highlight one bar in a series in highcharts?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • When should I choose bucket sort over other sorting algorithms?
  • Jquery - Jquery Wysiwyg return html as a string
  • AT Commands to Send SMS not working in Windows 8.1
  • Unanticipated behavior
  • Arrays break string types in Julia
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • WPF Applying a trigger on binding failure
  • Java static initializers and reflection
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal