How to generate a random string and compute the percent of a specific string?

Viewed 1042

I'm doing my first ever R course. One of the exercises is to create a random DNA strings with 1000 bases and for it compute the GC percentage (GC%).

I created the DNA bases vector and tried to create the sequence, however, the result is not correct

DNA <- c("A","G","T","C")
seq <- strrep(DNA, 250)

Any suggestion for this noob?

1 Answers

Basically you should check out very basics of R, i.e sum, sample, paste functions. The solution is quite simple:

DNA <- c("A","G","T","C")
chain <- sample(DNA, 1000, replace = T)
GCcontent <- sum(chain %in% c('G','C'))/length(chain)
# chain as a string
paste(chain, collapse="")
Related