Jorge Perez
Jorge Perez

Reputation: 11

R loop for creating and using -csv

I have a function output (from koRpus) of the form: Total number of tokens: 887 Total number of types: 393

Measure of Textual Lexical Diversity MTLD: 142.66 Number of factors: 6.22 Factor size: 0.72 SD tokens/factor: 41.55 (all factors) 38 (complete factors only)

And I want to make a loop for storing these results for 80 different documents. I have tried the following:

for (i in 1:length(infra$tableid)) {
  whypar <- paste(infra$whypar [infra[,1] ==i], collapse=" ")
wpi<- removeWords(whypar, stopwords("english"))

as.data.frame(wpi)
write.csv(data.frame(wpi), file= "wp.csv")

tagged.text <- tokenize("wp.csv", lang="en")

res.mtld <- MTLD(tagged.text)
  write.csv(data.frame(res.mtld),file="output.csv")
}

where infra is:

tableid 1, 2, 3, ... 80
whypar "I took part because xxx", "I believe that jshfdjk", "jhsadkjhd" ... (N=350)

Thanks for any help

Upvotes: 1

Views: 169

Answers (1)

Simon O&#39;Hanlon
Simon O&#39;Hanlon

Reputation: 59980

Extract the parts of the MTLD object you are interested in first. From your question it seems like you are only interested in a subset of the object returned by MTLD, namely the MTLD score, number of factors the SD of tokens/factor and the SD for complete factors only. If you only want these results for each file you can just write one nice table as your output for all the files:

res <- data.frame( ID = numeric() , MTLD=numeric() , Factor_Size=numeric() , SD=numeric() , SD_Complete=numeric() )
for (i in 1:length(infra$tableid)) {
  whypar <- paste(infra$whypar [infra[,1] ==i], collapse=" ")
  wpi<- removeWords(whypar, stopwords("english"))

  wpi <- as.data.frame(wpi)
  write.csv(data.frame(wpi), file= "wp.csv")

  tagged.text <- tokenize("wp.csv", lang="en")

  res.mtld <- MTLD(tagged.text)
  mtld <- res.mtld@MTLD$MTLD
  fac.size <-  res.mtld@param$factor.size
  mtld.sd <- res.mtld@MTLD$lengths$sd
  mtld.sd.compl <- res.mtld@MTLD$lengths$sd.compl
  res <- rbind( res , c( infra$tableid[i] , mtld, fac.size , mtld.sd , mtld.sd.compl ) )
}
  write.csv( res , file="output.csv" )

I hope this helps, but check these are the results you want returned.

Upvotes: 1

Related Questions