I have a dataframe with the following format.
A B
xxx 100;2;30;5
yyy 30;5
zzz 35
How to count the number of numbers in column B in second column and convert to the count as follows:
A B
xxx 4
yyy 2
zzz 1
Thanks.
Assuming your data are in a data.frame named Data, a combination of strsplit and sapply make short work of this.
Data$C <- sapply(strsplit(Data$B, ";"), length)
strsplit is vectorized, so it splits each element of column Data$B by ";" and returns a list of vectors. The list has one element for each row in Data and each list element contains a vector (e.g. "100;2;30;5" is converted to c("100","2","30","5")). The sapply call returns the length of each vector in the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With