Sebastian Zeki
Sebastian Zeki

Reputation: 6874

how to extract data from a list in R

I think the question is correctly phrased but I'm not sure I have a function which basically calculates a agreement statistic (kappa) between two columns in a series of dataframes. The problem is that the output is a list of lists (I think) so I'm not sure how to get the values I want. Ideally I would like to plot value versus the list name (total..)

Here is the function

lst <- mget(ls(pattern='total\\d+'))

classify_cnv = function (column)
  ifelse(column < 2, 1, ifelse(column > 2, 3, 2))

classify_all_cnvs = function (df) {
  df$CopyNumber.x = classify_cnv(df$CopyNumber.x)
  df$CopyNumber.y = classify_cnv(df$CopyNumber.y)
  df
}
result = lapply(lst, classify_all_cnvs)

more<-lapply(result, function(kv){
kappa2(kv[,c(5,8)], "squared")})

the resulting output is

....
$total7
 Cohen's Kappa for 2 Raters (Weights: squared)

 Subjects = 601 
   Raters = 2 
    Kappa = 0.02 

        z = 0.624 
  p-value = 0.533 

$total8
 Cohen's Kappa for 2 Raters (Weights: squared)

 Subjects = 620 
   Raters = 2 
    Kappa = 0.219 

        z = 7.27 
  p-value = 0.000000000000352 
....

str(more) gives me

  $ total7 :List of 8
  ..$ method   : chr "Cohen's Kappa for 2 Raters (Weights: squared)"
  ..$ subjects : int 601
  ..$ raters   : int 2
  ..$ irr.name : chr "Kappa"
  ..$ value    : num 0.02
  ..$ stat.name: chr "z"
  ..$ statistic: num 0.624
  ..$ p.value  : num 0.533
  ..- attr(*, "class")= chr "irrlist"
 $ total8 :List of 8
  ..$ method   : chr "Cohen's Kappa for 2 Raters (Weights: squared)"
  ..$ subjects : int 620
  ..$ raters   : int 2
  ..$ irr.name : chr "Kappa"
  ..$ value    : num 0.219
  ..$ stat.name: chr "z"
  ..$ statistic: num 7.27
  ..$ p.value  : num 0.000000000000352
  ..- attr(*, "class")= chr "irrlist"

I'd like to end up with a simple dataframe with two columns, one for the name of the parent list (total..) and the other for the value.

Upvotes: 0

Views: 220

Answers (1)

Ricky
Ricky

Reputation: 4686

I'm guessing the "value" you meant is the field value in your list.

df <- data.frame(name=names(more),
                 value=sapply(more, function(x) x$value))

creates a data frame with this as content

> df
         name value
total7 total7 0.020
total8 total8 0.219

Upvotes: 1

Related Questions