Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Count and convert to number

Tags:

r

count

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.

like image 763
user2718 Avatar asked Feb 24 '23 18:02

user2718


1 Answers

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.

like image 115
Joshua Ulrich Avatar answered Feb 26 '23 22:02

Joshua Ulrich