我正在尝试创建一个功能,它接受一个列表并选择前250个最低项。但是,当我运行完整代码时,有一个错误,所以我认为错误可能在函数中的某个地方选择最低的条件。
以下是代码:
selFun <- function(x)
{x == (order(x, decreasing = FALSE)[1:250])
}
这是我想要实现为x:
的一些数据列表 TSPAN6 TNMD DPM1 SCYL3 C1orf112 FGR CFH
9.533633e-01 2.546213e-05 2.868157e-03 2.519265e-02 7.661085e-01 3.308286e-02 7.419554e-09
FUCA2 GCLC NFYA STPG1 NIPAL3 LAS1L ENPP4
8.119698e-11 3.273434e-03 6.048925e-04 2.264826e-03 2.319325e-02 3.065685e-03 4.675599e-02
答案 0 :(得分:1)
您可以使用<section class="home-actu">
<div class="row">
<?php
$the_query = new WP_Query('showposts=3');
while ($the_query->have_posts()) :
$the_query->the_post();
?>
<article class="home-actu-article one-half column" style="background:url('<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?> <?php echo $url ?>');">
<div class="home-actu-article-top">
<h2><a href="<?php the_permalink() ?>"><?php the_title();?></a></h2>
<div class="details-home-actu">
<a href="#" target="_blank"><?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
$slug = $categories[0]->slug;
switch($slug)
{
case "slugcat":
echo "<img src=\"....\">";
break;
default:
break;
}
}
?></a>
<a href="#" target="_blank">stuff here</a>
</div>
</div>
<div class="home-actu-article-bottom-wrap">
<span class="home-actu-article-excerpt"><?php the_excerpt();?></span>
<span class="home-actu-article-bottom"><a href="<?php the_permalink() ?>">En savoir <span>+</span></a></span>
<div class="social-home-actu">
<a href="#" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a>
<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink() ?>&t=<?php the_title(); ?>"><i class="fa fa-facebook" aria-hidden="true"></i></a>
</div>
</div>
</article>
<?php // End of the loop.
endwhile;
?>
</div>
</section>
作为
order
或selFun <- function(x) {
x <- x[order(x, decreasing = FALSE)[1:250]]
x
}
sort
提取最低250个值。
注意:您在Q中使用了selFun <- function(x) {
x <- sort(x, decreasing = FALSE)[1:250]
x
}
。要为x指定值,请使用x == ....
。有关详细信息,请查看<-
和?Comparison
此外,您必须在函数中返回结果。这可以通过?assignOps
或通过简单地将对象放在最后来实现(在这些示例中,我只是将return(x)
放在函数的最后一行中)。
<强>测试强>
使用10值向量(并减少返回5的位数):
x
两个函数都返回
x <- 10:1
修改强>
如果你想创建一个指示这250个最低值的逻辑向量,你可以使用这些值:
selFun(x)
# [1] 1 2 3 4 5
给出(用上面提到的10个值的例子):
x %in% selFun(x)
答案 1 :(得分:0)
您也可以使用dplyr包和arrange
。我非常热衷于dplyr包:
arrange(iris, Petal.Length)[1:10, ]