Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Highlighted
Staff (Retired)

Re: Count number of occurrences of specific words in a string

It looks like your data is numeric (base 10 already!) so you might need to use char(value) to get it back to a string representation.

Craige
Highlighted
Staff

Re: Count number of occurrences of specific words in a string

Please find one of the alternatives. Run the script to make a table.

``````New Table( "Counting Ones",
New Column( "Number",
Numeric,
"Continuous",
Format( "Best", 17 ),
Set Values( [111000010100001, 10101, 11111, 0, 101010101] ),
Set Display Width( 132 )
),
New Column( "Count of One",
Numeric,
"Ordinal",
Format( "Best", 12 ),
Formula(
Length( Char( :Number ) ) - Length(
Substitute( Char( :Number ), "1", "" )
)
),
Set Display Width( 102 )
)
)``````
Highlighted
Super User

Re: Count number of occurrences of specific words in a string

Two more alternatives.

Use the Words() function with an empty string as delimiter:

``````b = 111001010;
N Row(Loc(Words(Char(b), ""), "1"));``````

Or just with math (i.e. without converting number into string). For example:

``````b = 111001010;
Sum(Mod(Round(b / 10 ^ (Floor(Log10(b + 1)) :: 0)), 10));
``````

Highlighted
Staff (Retired)

Re: Count number of occurrences of specific words in a string

I like that last one! It looks like it will actually work up to 16 bits; 17 consecutive ones can't be stored in a double precision number. The :: operator makes a matrix, and there is a lot of matrix math after that.

The words function might be the best one for general use...being easy to explain...

Craige
Article Labels

There are no labels assigned to this post.