I am having problems implementing this pseudocode in R language: I paste here both:
Pseudocode:
FrequentWords(Text, k)
FrequentPatterns ← an empty set
for i ← 0 to |Text| − k
Pattern ← the k-mer Text(i, k)
Count(i) ← PatternCount(Text, Pattern)
maxCount ← maximum value in array Count
for i ← 0 to |Text| − k
if Count(i) = maxCount
add Text(i, k) to FrequentPatterns
remove duplicates from FrequentPatterns
return FrequentPatterns
R Code:
PatternCount <- function(text, pattern){
times <- 0
for (i in 1:(nchar(text) - nchar(pattern))) {
if (substr(text, i, i + nchar(pattern)-1) == pattern)
times <- times + 1}
return(times)
}
FrequentWords <- function(text, k) {
FrequentPatterns <- list()
Count <- list()
maxCount <- 0
for (i in 1:(nchar(text) − k)) {
Pattern <- substr(text, i, i + k - 1)
Count[[i]] <- PatternCount(text, Pattern)
maxCount <- lapply(Count, which.max)
if (Count[[i]] == maxCount){
FrequentPatterns <- substr(text, i, i + k - 1)
FrequentPatternsNoDuplicates <-
FrequentPatterns[!duplicated(FrequentPatterns)]
print(FrequentPatternsNoDuplicates)
}}}
When I tried to run it says: "In if (Count[[i]] == maxCount) { ... : the condition has length > 1 and only the first element will be used.."
Thanks a million!
Aucun commentaire:
Enregistrer un commentaire