BigDawg007
BigDawg007

Reputation: 11

How to tally elements in a cell

(I am using r Studio)

I am doing a lit review where I record gene variants and then record the paper ID from which the variant was recorded from. I want to be able to count the number of papers each variant has like a tally:

For example in the first row of column PMID, there are 4 papers, so I want my output for that specific cell to be 4, and for the next cell below to be 5, and below that to be 3.

If anyone could help with that'd be greatly appreciated!

Dataframe "gene" column "Pmid" enter image description here

Upvotes: 1

Views: 49

Answers (1)

nniloc
nniloc

Reputation: 4243

You could use strsplit and lengths

df <- data.frame(PMID = c("258,234,212", "234,235,256,265"))

df$counts <- lengths(strsplit(df$PMID, ","))

df

#-----
             PMID counts
1     258,234,212      3
2 234,235,256,265      4

Upvotes: 1

Related Questions