Ashi
Ashi

Reputation: 61

table() is jumbling up rows in R while counting frequency

I have a dataframe my_data which looks like this:

LR_ID             Kmer           ProcID
  1             GTACGTAT         10
  1             TACGTATC         10
  1             ACGTATCG          2
  1             GTATCGTT          3
  2             GTTACGTA         16
  2             TTACGTAC         16
  2             TACGTACT         16
  2             ACGTACTT         11

There are 800 LR Ids like these which have kmers going to different processes. I want to count the frequency based on LR_ID so I have used:

freq_my_data <- data.frame(table(my_data$LR_ID))

The output I am getting is this:

Var1    Freq
 1     27248
 10    8677
 100   13439
 101   18380
 102   2276
 103   1754

But I want the rows to be coming like LR1, LR2, and so on. I have tried to sort the data using order() but still it is giving me the same output.

Can someone give me any idea where I am going wrong.

Upvotes: 0

Views: 23

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389225

Var1 column is probably character/factor. Convert it to number and then use order to sort.

freq_my_data$Var1 <- as.numeric(as.character(freq_my_data$Var1))
freq_my_data <- freq_my_data[order(freq_my_data$Var1), ]

Upvotes: 1

Related Questions