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.
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
Post a Comment