This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to calculate how many identical elements are in each line of a file

hello everyone. I have a table similar to the following:

gene1 A B A A A C 
gene2 C A B A A A 
gene3 A B A C A B

I want to calculate the number of A, B and C in each row from this table. Try to get the following results (representing how many times each letter appears in this line):

gene1 A4 B1  C1
gene2 A4 B1  C1 
gene3 A3 B2  C1 

Any help will be appreciated ^_^

code calculation

1 answer

in R:

as.matrix(read.table("input.tsv",row.names = 1)) -> cnts
t(apply(t(cnts),2,function(x){paste(names(table(x)),as.integer(table(x)),sep="")}))
      [,1] [,2] [,3]
gene1 "A4" "B1" "C1"
gene2 "A4" "B1" "C1"
gene3 "A3" "B2" "C1"

very thanks, i will be try it

Log in to answer this question.