Fatima
Fatima

Reputation: 495

count words with for loop in R

I use R for text mining in Arabic language and I would like to check on words if it the word have more than 6 character do the some change

and it is working but it return the first word only here is my code

LL<- "بنزين سائقين تعملين مخينعو ينام"
    n2<- length(LL)
    for (i in 1:n2 ){
         for (j in 1:n2){
            o[j] <-(strsplit(LL[i], " "))
                    K<-ifelse(nchar(o[[j]][j])>=6 ,gsub('(?<=\\p{L})\\x{064A}\\x{0646}$', '', o[[j]][j], perl = TRUE),o[[j]][j])
                    return( print(K))
         } }

  [1] "بنزين"

and I think I need to add j++ but I don’t know where should I add it thank you

Upvotes: 1

Views: 302

Answers (1)

niko
niko

Reputation: 5281

You can perform that task without any loop

words <- unlist(strsplit(LL, " "))
nchar(words)
# returns
[1] 5 6 6 6 4

The rest could probably also be performed using vectorization e.g.

K <- character(length(words))
K[nchar(words) < 6] <- words[nchar(words) < 6]
K[nchar(words) > 5] <- gsub('(?<=\\p{L})\\x{064A}\\x{0646}$', '', 
                            words[nchar(words) > 5], perl = TRUE)
# result
K
[1] "بنزين"  "سائق"   "تعمل"   "مخينعو" "ينام"  

On a side note:

  • you are using return inside a loop - I do not think that is the proper way to use it. A simple print(...) would suffice.
  • also, whenever a result is not of expected size and ifelse is involved, you might want to check the length of the condition statement as that will determine the size of the result.

Upvotes: 2

Related Questions