This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How can I count the alphabat of column 2 against similar number of column one in R

I have the data in two columns C1 and C2.

C1  C2
1   a      
1   a
1   x  
1   z 
2   a
2   y 
3   x
3   x
3   x
3   z
..

I want the output in the form how many time alphabet occurs against a specific number of C1.

For example select all the number "1" from column one and then search how many time alphabet occurs against the selected number. and then go for the next umber of column 1 do the same thing and so on till last.

1   a(2)      
    x(1)
    z(1)  
2   a(1)      
    y(1) 
3   x(3)      
    z(1)
..

Please suggest me how can I get the desired output.

Thanks in advance

loop count

What have you tried? There are multiple ways to do this sort of thing, try a few things that come to your mind and then ask for help if the performance sucks or you can't get things to work properly.

I tried it in R with the following:

group_by(t38kbat, query_id, gene) %>% summarise(n())</td>

I receieved the output in this form

query_id  gene n()
1  CSAI_contig04661_6  GCST   3
2  CSAI_contig04661_6 SMC6A   1
3  CSAI_contig04661_6 Y1099   1
4  CSAI_isotig00001_4 AMSH3   1
5  CSAI_isotig00001_4 C98A9   1
6  CSAI_isotig00001_4 MOB2A   1
7  CSAI_isotig00001_4 PP299   1
8  CSAI_isotig00001_4  QORL   1
9  CSAI_isotig00001_4 WAKLP   1
10 CSAI_isotig00004_3  GCST   1
..                ...   ... ...

I want to print query id only one . For example

CSAI_contig04661_6
                                               GCST   3
                                               SMC6A   1
                                               Y1099   1

CSAI_isotig00001_4
                                               AMSH3   1
                                                C98A9   1
                                                MOB2A   1
                                                PP299   1
                                                QORL   1
                                                WAKLP   1

So iterate over the rows and just don't print the first cell if it matches that from the previous row. You could do this with a "for loop" or with split() and then lapply() (this method might be faster).

I tried to count the how many time a gene is present against the query ID with the loop. But its not working. I am absolutely new in programming If I had made some mistake I hope you will forgive me. I need your suggestions to correct it.

testfile<-s_t38kbat
head(testfile)
col1<- trialfile$query_id
col2<- trialfile$gene
counting_columns(col1,col2)
length(col1)==length(col2)
length(col1) > 0
length(col2) > 0
col_element = col1[0]
start=0
end=0
large_dick = {}
for (i in (0:length(col1)))  element = col1[i] {
  if (element != col1_element) {
    start=end
    end=i
  }
  large_dick[col1_element] = count_unique_elements(col2, range(start:end)
  col1_element=element
  if (end < length(col1))
  large_dick[col1_element] = count_unique_elements(col2, range(end:length(col1))
}
return large_dick

count_unique_elements(col2, start, end)
disk{}
for (i in range(start,end)) element= col2[i] {
  if (disk has_key(element)) {
    val= disk.get(element)
    val=val+1
    disk[element] = val
  }
  else 
    disk[element] = 1
}
return(disk)

print(counting_columns(cat1,cat2))
}

Hello tcf.hcdg!

We believe that this post does not fit the main topic of this site.

Not a bioinformatics question, no effort put into finding a solution

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

0 answers

No answers yet.

Log in to answer this question.