How to calculate how many identical elements are in each line of a file
1
1
Entering edit mode
2.2 years ago
yoser4 ▴ 10

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 • 609 views
ADD COMMENT
0
Entering edit mode
2.2 years ago

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"
ADD COMMENT
0
Entering edit mode

very thanks, i will be try it

ADD REPLY

Login before adding your answer.

Traffic: 2674 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