简体   繁体   English

如何从R中的列表中提取数据

[英]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. 我认为该问题的措词正确,但是我不确定我是否具有一个函数,该函数基本上可以计算一系列数据帧中两列之间的一致性统计(kappa)。 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 str(more)给我

  $ 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. 我想以一个简单的数据框结束,该数据框具有两列,一列用于父级列表的名称(总计),另一列用于值。

I'm guessing the "value" you meant is the field value in your list. 我猜您的意思是列表中的字段value的“值”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM