A for loop with strsplit in R error

324 Views Asked by At

I'd like to preface this post to say that I am new to R and bioinformatics coding, and that I'd really appreciate some input from this knowledgable community. My goal for the code posted below is to generate pie charts that show amino acid abundance per protein from BLAST results. I uploaded a csv file from UniProt, converted it to a matrix, and wrote out the code below. I keep getting the error: In AAs[i] = table(strsplit(BLAST_AA_seqs[i], "", useBytes = TRUE)) :number of items to replace is not a multiple of replacement length. Column 8 is the output column that contains the amino acid sequences. Thanks in advance!

mydata=read.table("CDPKbeta_BLAST_results.csv",header=TRUE,sep=",")
mydata=as.matrix(mydata)

AAs=c()
BLAST_AA_seqs=c()
for(i in 1:nrow(mydata)){
  print(i)
  BLAST_AA_seqs[i]=mydata[i,8]
  AAs[i]=table(strsplit(BLAST_AA_seqs[i],"", useBytes=TRUE))
  pie(AAs, col=rainbow(length(AAs)), main="Residue abundance")
  }
1

There are 1 best solutions below

0
On
lal<-c()
lal[1]=table(strsplit("", "", useBytes = T))

empty string is the problem (BLAST_AA_seqs[i] is empty string)