如何通过r中的列表中的数据类型忽略元素?

问题描述 投票:0回答:2

我有如下示例数据;

所需的数据类型是double,但还有其他类型,例如list。如何删除或忽略它们?

       sample<-list(x1 = structure(c(0.266, 0.532, 0.797, 1.092, 1.27, 1.27, 
1.27, 1.46, 1.46, 2.34, 2.53, 2.53, 2.53, 2.53), .Dim = c(14L, 
1L), .Dimnames = list(c("m5", "m10", "m15", "m30", "h1", "h2", 
"h3", "h4", "h5", "h6", "h8", "h12", "h18", "h24"), NULL)), x2 = structure(c(1.224, 
2.395, 3.063, 5.131, 7.112, 9.474, 9.474, 10.302, 10.744, 9.474, 
12.49, 11.406, 13.571, 13.919), .Dim = c(14L, 1L), .Dimnames = list(
    c("m5", "m10", "m15", "m30", "h1", "h2", "h3", "h4", "h5", 
    "h6", "h8", "h12", "h18", "h24"), NULL)), x3 = list(1, 2, 
    3))
>sample
$x1
     [,1]
m5  0.266
m10 0.532
m15 0.797
m30 1.092
h1  1.270
h2  1.270
h3  1.270
h4  1.460
h5  1.460
h6  2.340
h8  2.530
h12 2.530
h18 2.530
h24 2.530

$x2
      [,1]
m5   1.224
m10  2.395
m15  3.063
m30  5.131
h1   7.112
h2   9.474
h3   9.474
h4  10.302
h5  10.744
h6   9.474
h8  12.490
h12 11.406
h18 13.571
h24 13.919

$x3
$x3[[1]]
[1] 1

$x3[[2]]
[1] 2

$x3[[3]]
[1] 3

所需的输出:

$x1
     [,1]
m5  0.266
m10 0.532
m15 0.797
m30 1.092
h1  1.270
h2  1.270
h3  1.270
h4  1.460
h5  1.460
h6  2.340
h8  2.530
h12 2.530
h18 2.530
h24 2.530

$x2
      [,1]
m5   1.224
m10  2.395
m15  3.063
m30  5.131
h1   7.112
h2   9.474
h3   9.474
h4  10.302
h5  10.744
h6   9.474
h8  12.490
h12 11.406
h18 13.571
h24 13.919
r ignore
2个回答
1
投票

您可以使用purrr包中的keep函数。这有帮助吗?

install.packages("purrr")
library(purrr)

new_list <- keep(sample, is.double)
new_list

如果您也可以显示所需的输出,太好了,谢谢


0
投票

base R中,我们可以使用Filter

sample_new <- Filter(is.double, sample)
© www.soinside.com 2019 - 2024. All rights reserved.