Search code examples
sortingfor-loopquantedafrequency-analysis

how to sort a list of doubles according to their names in R


I am trying to write a function to calculate R1 lexical richness measure. The formula is as follows:

R1 = 1 - ( F(h) - h*h/2N) )

where N is the number of tokens, h is the Hirsch point, and F(h) is the cumulative relative frequencies up to that point. my actual data is in the same format as the data below:

txt <- list( 
  a = c("The truck driver whose runaway vehicle rolled into the path of an express train and caused one of Taiwan’s worst ever rail disasters has made a tearful public apology.", "The United States is committed to advancing prosperity, security, and freedom for both Israelis and Palestinians in tangible ways in the immediate term, which is important in its own right, but also as a means to advance towards a negotiated two-state solution.","The 49-year-old is part of a team who inspects the east coast rail line for landslides and other risks.", "We believe that this UN agency for so-called refugees should not exist in its current format.","His statement comes amid an ongoing investigation into the crash, with authorities saying the train driver likely had as little as 10 seconds to react to the obstruction.", " The US president accused Palestinians of lacking “appreciation or respect.", "To create my data I had to chunk each text in an increasing manner.", "Therefore, the input is a list of chunked texts within another list.","We plan to restart US economic, development, and humanitarian assistance for the Palestinian people,” the secretary of state, Antony Blinken, said in a statement.", "The cuts were decried as catastrophic for Palestinians’ ability to provide basic healthcare, schooling, and sanitation, including by prominent Israeli establishment figures.","After Donald Trump’s row with the Palestinian leadership, President Joe Biden has sought to restart Washington’s flailing efforts to push for a two-state resolution for the Israel-Palestinian crisis, and restoring the aid is part of that.")
           )
library(quanteda)

DFMs <- lapply(txt, dfm)
txt_freq <- function(x) textstat_frequency(x, groups = docnames(x), ties_method = "first")
Fs <- lapply(DFMs, txt_freq)

get_h_point <- function(DATA) {
  fn_interp <- approxfun(DATA$rank, DATA$frequency)
  fn_root <- function(x) fn_interp(x) - x
  uniroot(fn_root, range(DATA$rank))$root
}

s_p <- function(x){split(x,x$group)}
tstat_by <- lapply(Fs, s_p)
h_values <-lapply(tstat_by, vapply, get_h_point, double(1))

str(tstat_by)
str(h_values)
F <- list()
R <- list()
temp <- list()
for( Ls in names(tstat_by) ){  
  for (item in names(h_values[[Ls]]) ){  
    temp[[Ls]][[item]] <-  subset(tstat_by[[Ls]][[item]], rank <= h_values[[Ls]][[item]])
    F[[Ls]][[item]] <- sum(temp[[Ls]][[item]]$frequency) / sum(tstat_by[[Ls]][[item]]$frequency) 
    R[[Ls]][[item]] <- 1 - ( F[[Ls]][[item]] - 
                               h_values[[Ls]][[item]] ^ 2 / 
                               2 * sum(tstat_by[Ls][[item]]$frequency) )
  }}

I have the value I need stored in a list but in the wrong order. here is what the for loop produces:

 names(R[["a"]])
 [1] "text1"  "text10" "text11" "text2"  "text3"  "text4"  "text5"  "text6"  "text7" 
[10] "text8"  "text9" 

but I need it to be in this natural order:

 names(R[["a"]])
  [1] "text1"  "text2"  "text3"  "text4"  "text5"  "text6"  "text7" "text8"  "text9"
 [10] "text10" "text11"

so the question is how do I get the values sorted based on the names they have—the numeric parts of the names need to be in order.


Solution

  • Order them by the integer values in the element names, after stripping the "text" part.

    > R$a <- R$a[order(as.integer(gsub("text", "", names(R$a))))]
    > R$a
    $text1
    [1] 0.8666667
    
    $text2
    [1] 0.8510638
    
    $text3
    [1] 0.9
    
    $text4
    [1] 0.9411765
    
    $text5
    [1] 0.8333333
    
    $text6
    [1] 0.9166667
    
    $text7
    [1] 0.8666667
    
    $text8
    [1] 0.8571429
    
    $text9
    [1] 0.7741935
    
    $text10
    [1] 0.8888889
    
    $text11
    [1] 0.8717949