如何连续从列表中提取数据

时间:2019-02-15 23:44:59

标签: r list

我有一个像这样的矩阵

df1 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df2 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df3 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df4 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df5 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df6 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df7 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df8 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df9 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df10 <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))
df <- rbind(df1,df2,df3,df4,df5,df6,df7,df8,df9,df10)

我有一个像这样的向量

dft <- sample(seq(1,10), size=100, replace=TRUE, prob=c(.02,.01,.01,.01,.01,.01,.005,.005,.01,.01))

然后我对这样的数据进行测试

t<- sapply(1:nrow(df), function(i) ks.test(as.vector(df[i,]), as.vector(dft)))

我有一个名为 t 的列表文件,该文件为我提供了D值和p.values,我想提取它们并在它们超过100时对其进行绘制。有没有办法做到这一点呢?给他们每个人一个?列表的结构如下所示 str(t)

List of 50
 $ : Named num 0.09
  ..- attr(*, "names")= chr "D"
 $ : num 0.813
 $ : chr "two-sided"
 $ : chr "Two-sample Kolmogorov-Smirnov test"
 $ : chr "as.vector(df[i, ]) and as.vector(dft)"
 $ : Named num 0.11
  ..- attr(*, "names")= chr "D"
 $ : num 0.581
 $ : chr "two-sided"
 $ : chr "Two-sample Kolmogorov-Smirnov test"
 $ : chr "as.vector(df[i, ]) and as.vector(dft)"
 $ : Named num 0.09
  ..- attr(*, "names")= chr "D"

我可以看到列表的长度是

length(t)
[1] 377930

我想提取每两个数据,而将其余数据放在一个数据帧中。

我确实喜欢这样

c(t[[1]],t[[2]])
c(t[[6]],t[[7]])
c(t[[11]],t[[12]])
c(t[[21]],t[[22]])
c(t[[26]],t[[27]])
c(t[[31]],t[[32]])
c(t[[36]],t[[37]])

是否有更好的方法从上述列表中提取数据?

我也尝试使用以下方法做到这一点,但未成功

result<- data.frame(matrix(NA, nrow = length(t), ncol = 1))
m <- seq(1,length(t),by=5)
for (i in seq_along(m)){
  result[[i]] = c(t[[i]]) 
  if ( i*2 > length(t) ){
    break
  }
}

1 个答案:

答案 0 :(得分:2)

t的结构是具有指定长度的重复模式,如果将其转换为矩阵,则可以更轻松地使用它:

t_matrix <- matrix(t, ncol=5, byrow=T)

t_matrix
      [,1] [,2]      [,3]        [,4]                                 [,5]                                   
 [1,] 0.11 0.5806178 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"
 [2,] 0.08 0.9062064 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"
 [3,] 0.11 0.5806178 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"
 [4,] 0.08 0.9062064 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"
 [5,] 0.04 0.9999982 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"
 [6,] 0.05 0.9996333 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"
 [7,] 0.15 0.2105516 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"
 [8,] 0.08 0.9062064 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"
 [9,] 0.08 0.9062064 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"
[10,] 0.1  0.6993742 "two-sided" "Two-sample Kolmogorov-Smirnov test" "as.vector(df[i, ]) and as.vector(dft)"

通过指定byrow=T,R将按行而不是按默认方式按列将数据加载到5列矩阵中。现在有了矩阵,就可以像其他矩阵或数据框一样对它进行子集处理:

t_matrix[,c(1,2)]
      [,1] [,2]     
 [1,] 0.11 0.5806178
 [2,] 0.08 0.9062064
 [3,] 0.11 0.5806178
 [4,] 0.08 0.9062064
 [5,] 0.04 0.9999982
 [6,] 0.05 0.9996333
 [7,] 0.15 0.2105516
 [8,] 0.08 0.9062064
 [9,] 0.08 0.9062064
[10,] 0.1  0.6993742