开发者

R: Count and convert to number

开发者 https://www.devze.com 2023-03-16 11:37 出处:网络
I have a dataframe with the following format. AB 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:

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

Thank开发者_Python百科s.


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.


This does the trick:

dfr$B<-nchar(as.character(dfr$B))-nchar(gsub(";","",dfr$B))+1

Edit: I think this should be slightly faster:

dfr$B<-nchar(as.character(dfr$B))-nchar(gsub(";","",dfr$B, fixed=TRUE))+1
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号