how can I count the strings and set a number?

Solution 1:

Try table

> table(df)
df
TNFM00000001497 TNFM00000001617 TNFM00000001626 TNFM00000001629 TNFM00000001630
              1               4               4               2               3

or

> as.data.frame(table(df))
               df Freq
1 TNFM00000001497    1
2 TNFM00000001617    4
3 TNFM00000001626    4
4 TNFM00000001629    2
5 TNFM00000001630    3

or

> stack(table(df))
  values             ind
1      1 TNFM00000001497
2      4 TNFM00000001617
3      4 TNFM00000001626
4      2 TNFM00000001629
5      3 TNFM00000001630

Solution 2:

Another way would be:

aggregate(df$Mydf, list(freq = df$Mydf), length)

# freq x
# 1 TNFM00000001497 1
# 2 TNFM00000001617 4
# 3 TNFM00000001626 4
# 4 TNFM00000001629 2
# 5 TNFM00000001630 3

tapply can work here as well:

tapply(df$Mydf, df$Mydf, length)

Solution 3:

Use group_by and count:

library(dplyr)

df %>% 
  group_by(String = Mydf) %>% 
  count()

# A tibble: 5 x 2
# Groups:   String [5]
  String              n
  <chr>           <int>
1 TNFM00000001497     1
2 TNFM00000001617     4
3 TNFM00000001626     4
4 TNFM00000001629     2
5 TNFM00000001630     3

Solution 4:

Another possible solution:

library(tidyverse)

df %>% add_count(Mydf) %>% distinct

#>              Mydf n
#> 1 TNFM00000001497 1
#> 2 TNFM00000001617 4
#> 3 TNFM00000001626 4
#> 4 TNFM00000001629 2
#> 5 TNFM00000001630 3