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

问题描述 投票:1回答:1

我有一个像这样的矩阵

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
  }
}
r list
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.