How to count the same character string in R
2
0
Entering edit mode
2.3 years ago
j_weld ▴ 10

I have different cancer types like this (the unique cancer types are 36). I want to know the number of each cancer type.

R • 970 views
ADD COMMENT
2
Entering edit mode
$ summary(as.factor(cancers))

cancers is the vector storing values.

ADD REPLY
3
Entering edit mode
2.3 years ago

table from base R, or count from the dplyr library will do this.

ADD COMMENT
2
Entering edit mode
2.3 years ago
seidel 11k

rpolicastro got the answer, but spelling it out explicitly here because table() is a great function for counting word or item frequencies, and it's fun to see the output:

> cancers <- c("CLL",
              "BLCA",
              "LUAD",
              "BLCA",
              "KIRC",
              "KIRC",
              "GBM")
> table(cancers)
cancers
BLCA  CLL  GBM KIRC LUAD 
   2    1    1    2    1 
ADD COMMENT

Login before adding your answer.

Traffic: 2985 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6