You can do
sapply(strsplit(subcat_id,","),FUN=function(x){length(x[x!="Null"])})
strsplit(subcat_id,",")
will return a list of each item in subcat_id split on commas. sapply
will apply the specified function to each item in this list and return us a vector of the results.
Finally, the function that we apply will take just the non-null entries in each list item and count the resulting sublist.
For example, if we have
subcat_id <- c("1,2,3","23,Null,4")
Then running the above code returns c(3,4)
which you can assign to your column.
If running this from a dataframe, it is possible that the character column has been interpreted as a factor, in which case the error
non-character argument will be thrown. To fix this, we need to force interpretation as a character vector with the
as.character
function, changing the command to
sapply(strsplit(as.character(frame$subcat_id),","),FUN=function(x){length(x[x!="Null"])})