Count of Comma separate values in r -


i have column named subcat_id in values stored comma seperated lists. need count number of values , store counts in new column . lists have null values want arid of.

example

i store counts in n column.

we can try

 nchar(gsub('[^,]+', '', gsub(',(?=,)|(^,|,$)', '',        gsub('(null){1,}', '', df1$subcat_id), perl=true)))+1l  #[1] 6 4 

or

library(stringr) str_count(df1$subcat_id, '[0-9.]+') #[1] 6 4 

data

 df1 <- data.frame(subcat_id = c('1,2,3,15,16,78',          '1,2,3,15,null,null'), stringsasfactors=false) 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -