Count Values Separated by a Comma in a Character String

Count values separated by a comma in a character string

These two approaches are each short, work on vectors of strings, do not involve the expense of explicitly constructing the split string and do not use any packages. Here d is a vector of strings such as d <- c("1,2,3", "5,2") :

1) count.fields

count.fields(textConnection(d), sep = ",")

2) gregexpr

lengths(gregexpr(",", d)) + 1

R: How to Count All Character Values Separated By Commas In A Column?

To count the frequency of ICD10Code in the entire column, we can split the string on comma, unlist it and count it with table.

table(unlist(strsplit(as.character(data.1$ICD10Code), ',')))

Count the number of elements in a string separated by comma

You can count the number of commas:

text.count(",") + 1
# 3

Count comma separated unique values in a string

We can use stri_extract to extract all the numbers, then loop through the list, find the length of unique elements

library(stringi)
df1$Count <- sapply(stri_extract_all_regex(df1$col3, "[0-9]+"),
function(x) length(unique(x)))

Counting comma separated string in dataframe in a new column

First you need to str.split the column on the comma, expand the result to create a dataframe, stack to get a series and use str.get_dummies that will create a column for each different word and add 1 for the corresponding value in the series. Finally sum on level=0 to go back to original shape. join the result to the original dataframe

df = df.join( df['Interaction'].str.split(',', expand=True)
.stack()
.str.get_dummies()
.sum(level=0)
)
print(df)
Name Interaction care hug like \
0 John share,like,share,like,like,like 0 0 4
1 Sara love,like,share,like,love,like 0 0 3
2 Paul share,like,share,like,like,like,share,like,sha... 0 1 7
3 Guest share,like,care,like,like,like 1 0 4

love share
0 0 2
1 2 1
2 0 4
3 0 1

Finding length of a character string which is separated by commas in R

Using @akrun's sample data, here's the count.fields approach I mentioned in the comments.

> count.fields(textConnection(DF$Values), sep = ",")
[1] 4 7 6

If they are factors, just use textConnection(as.character(DF$Values)) instead.

get count of string with commas and split them

This should be what you are looking for:

String str="plane,cat,red,dogy";
for(String subString: str.split(",")){
updatestatement(subString);
}

Get a count of ranged and comma separated items in a string in SQL

This can be cleaned up/optimized and is intentionally verbose but should get you started. Notably, the logic inside the last IF is almost identical to that of the WHILE and the block that gets the numeric value of the left/right elements is repeated four times.

declare @input varchar(max)
set @input = 'R1,R15-R19,RN5-RN6'

select @input

declare @elements table
(
Element varchar(10),
[Count] int
)

declare @element varchar(10)
declare @index int
declare @count int
declare @left varchar(10)
declare @right varchar(10)
declare @position int

while (len(@input) > 0 and charindex(',', @input) > 0)
begin
set @element = substring(@input, 0, charindex(',', @input))
if (charindex('-', @element) > 0)
begin
set @index = charindex('-', @element)
set @left = left(@element, @index - 1)
set @right = substring(@element, @index + 1, len(@element) - len(@left))

set @position = 0
while (isnumeric(substring(@left, @position, 1)) = 0)
begin
set @position = @position + 1
end
set @left = substring(@left, @position, len(@left))

set @position = 0
while (isnumeric(substring(@right, @position, 1)) = 0)
begin
set @position = @position + 1
end
set @right = substring(@right, @position, len(@right))

set @count = cast(@right as int) - cast(@left as int) + 1
end
else
begin
set @count = 1
end
insert into @elements select @element, @count
set @input = replace(@input, @element + ',', '')
end

if (len(@input) > 0)
begin
set @element = @input
if (charindex('-', @element) > 0)
begin
set @index = charindex('-', @element)
set @left = left(@element, @index - 1)
set @right = substring(@element, @index + 1, len(@element) - len(@left))

set @position = 0
while (isnumeric(substring(@left, @position, 1)) = 0)
begin
set @position = @position + 1
end
set @left = substring(@left, @position, len(@left))

set @position = 0
while (isnumeric(substring(@right, @position, 1)) = 0)
begin
set @position = @position + 1
end
set @right = substring(@right, @position, len(@right))

set @count = cast(@right as int) - cast(@left as int) + 1
end
else
begin
set @count = 1
end
insert into @elements select @element, @count
end

select * from @elements
select sum([Count]) from @elements

Outputs the following results:

R1,R15-R19,RN5-RN6

R1 1
R15-R19 5
RN5-RN6 2

8

Count column comma delimited values oracle

This uses simple string functions and a recursive sub-query factoring and may be faster than using regular expressions and correlated joins:

Oracle Setup:

CREATE TABLE account ( id, "user", title ) AS
SELECT 1, 'foo', 'a,b,c' FROM DUAL UNION ALL
SELECT 2, 'bar', 'a,d' FROM DUAL UNION ALL
SELECT 3, 'tee', 'b' FROM DUAL;

Query:

WITH positions ( title, start_pos, end_pos ) AS (
SELECT title,
1,
INSTR( title, ',', 1 )
FROM account
UNION ALL
SELECT title,
end_pos + 1,
INSTR( title, ',', end_pos + 1 )
FROM positions
WHERE end_pos > 0
),
items ( item ) AS (
SELECT CASE end_pos
WHEN 0
THEN SUBSTR( title, start_pos )
ELSE SUBSTR( title, start_pos, end_pos - start_pos )
END
FROM positions
)
SELECT item,
COUNT(*)
FROM items
GROUP BY item
ORDER BY item;

Output:


ITEM | COUNT(*)
:--- | -------:
a | 2
b | 2
c | 1
d | 1

db<>fiddle here



Related Topics



Leave a reply



Submit