如何获得任何列中的值大于值的行?

时间:2019-02-07 22:20:49

标签: r

我的数据是这样的:

> head(df)
   ETDPAT04 ETDPAT06 ETDPAT08 ETDPAT12
1:        2        .        3        3
2:       12       12        .       14
3:        6        5        6        7
4:        1        1        1        1
5:        1        3        3        2
6:        3        3        2        4
...

如何返回所有值大于61的行?

我试图这样做:

a=df[apply(df, 1, function(row) {any(row > 61)}),]

我得到的不满足上述条件。我知道了:

    > head(a)
   ETDPAT04 ETDPAT06 ETDPAT08 ETDPAT12
1:        6        5        6        7
2:        6        6        7        8
3:        8        3        6        4

...

在我的数据框中,那些列中没有超过61的数据,所以我应该得到零结果。

colMax <- function(df) sapply(df, max, na.rm = TRUE)
colMax(df)
ETDPAT04 ETDPAT06 ETDPAT08 ETDPAT12 
     "9"      "9"      "9"      "9" 

也:

> sapply(df, class)
   ETDPAT04    ETDPAT06    ETDPAT08    ETDPAT12 
"character" "character" "character" "character" 

我从以下位置获得了df:

t=data.table::fread("phs000086.v3.pht000279.v1.DS-T1D-IRB.txt", header=TRUE,na.strings = ".")
colnames(t) <- as.character(t[1,])
t <- t[2:nrow(t),]
df=select(t, ETDPAT04, ETDPAT06,ETDPAT08,ETDPAT12)
df <- sapply( df, as.numeric )
a=df[apply(df, 1, function(row) {any(row > 61)}),]
dim(a)
44  4
head(a)

     ETDPAT04 ETDPAT06 ETDPAT08 ETDPAT12
[1,]       NA       NA       NA       NA
[2,]       NA       NA       NA       NA

我的原始.txt数据如下:

       phv00033517.v1.p1.c1 phv00033518.v1.p1.c1 phv00033519.v1.p1.c1
1:                PHASE                  AGE                ADULT
2:                    2                   17                    0
3:                    2                   29                    1
4:                    2                   35                    1
5:                    2                   14                    0

我想删除第一行,并将第二行放入标题中,所以我的列名变成:PHASE,AGE ...

我也尝试以更基本的方式做到这一点,但仍然没有解决办法:

library(dplyr)
d<- read.table("phs000086.v3.pht000279.v1.p1.c1.DCCT_ms2exprt.DS-T1D-   IRB.txt", header = FALSE)
write.table(d,"phen2", quote=F,sep = " ",row.names = F,col.names=F)
d1=read.table("phen2", header=TRUE)
d2=select(d1,AGE, FEMALE,HBAEL,ETDPAT00, ETDPAT02, ETDPAT04, ETDPAT06, ETDPAT08, ETDPAT10, ETDPAT12)
d2[d2=="."]<-NA

asNumeric <- function(x) as.numeric(as.character(x))
factorsNumeric <- function(d) modifyList(d, lapply(d[, sapply(d, is.factor)],   
                                               asNumeric))
f <- factorsNumeric(d2)
f[4:9] <- lapply(f[4:9], as.integer)

a=f[apply(t(f[,4:10]>61),1, any), ]

我正在获得一个数据框a,其中包含800条或全部填充有NA的行。当我尝试查找任何值大于61的列时。 如果我寻找值> 61的任何行,得到所有NA的77行,则相同。

a=f[apply(t(f[,4:10]>61),2, any), ]

sapply(f, class)
      AGE    FEMALE     HBAEL  ETDPAT00  ETDPAT02  ETDPAT04  ETDPAT06     ETDPAT08 
"integer" "integer" "numeric" "integer" "integer" "integer" "integer"   "integer" 
 ETDPAT10  ETDPAT12 
"integer" "integer"

我完全被困在这里。任何人都可以提供帮助吗?我需要提供有关我的数据的更多信息吗?

1 个答案:

答案 0 :(得分:0)

假设<?php get_header(); ?> <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php while ( have_posts() ) : the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <div class="entry-content"> <?php if ( has_post_thumbnail() ) : ?> <?php the_post_thumbnail( 'photoline-big' ); ?> <?php endif; //has_post_thumbnail ?> <?php if ( has_excerpt() ) : ?> <header class="entry-header"> <?php the_excerpt(); ?> </header><!-- .entry-header --> <?php endif; //has_excerpt() ?> <h1 class="page-title"><?php the_title(); ?></h1> <?phpenter code here $args = array( 'post_type' => 'ag212_company', 'post_status' => 'publish', 'posts_per_page' => 0, 'orderby' => 'title', 'order' => 'ASC', 'meta_query' =>array( array( 'key' => '_branch', 'value' => 'larnaka', 'compare' => '=', ), ), ); ?> <?php echo 'Welcome to Agency 212. This is your start point please fill the required fields'; ?> <br> <table width= "100%" border = "10"> <tr> <td> <p> <b> Welcome to Agency 212</b> </p> <form action="<?php echo the_permalink();?>" method="get"> <tr> <td> <p> <b> Please select Your company </b> </p> <select name="qv_company"> <?php $args = array( 'post_type' => 'ag212_company', 'post_status' => 'publish', 'posts_per_page' => 0, 'orderby' => 'title', 'order' => 'ASC', 'meta_query' =>array( array( 'key' => 'company_branch', 'value' => 'Nikis', 'compare' => '=', ), ), ); //get posts from db, using args above $loop = new WP_Query( $args ); ?> <?php if($loop->have_posts()){ while ( $loop->have_posts() ) : $loop->the_post(); global $post; manipulate data in post <option value="[company_name]">[company_name]</option> echo '<option value="'.get_post_meta($post->ID, 'company_name', true ).'">'.get_post_meta($post->ID, 'company_name', true ).'</option>'; endwhile; } else { echo '<option value="false">No companies found</option>'; } wp_reset_postdata(); ?> </select> <button type="button">ADD Company</button> </td> </tr> <tr> <td> <p> <b> Please selelect your branch </b> </p> <select name="qv_branch"> <?php $args = array( 'post_type' => 'ag212_branch', 'post_status' => 'publish', 'posts_per_page' => 0, 'orderby' => 'title', 'order' => 'ASC', 'meta_query' =>array( array( 'key' => 'company_branch', 'value' => 'limassol', 'compare' => '=', ), ); get posts from db, using args above $loop = new WP_Query( $args ); ?> <?php loop through available posts if they exist if($loop->have_posts()){ while ( $loop->have_posts() ) : $loop->the_post(); global $post; manipulate data in post <option value="[company_name]">[company_name]</option> echo '<option value="'.get_post_meta($post->ID, 'branch_name', true ).'">'.get_post_meta($post->ID, 'branch_name', true ).'</option>'; endwhile; } else { echo '<option value="false">No branch found</option>'; } wp_reset_postdata(); ?> </select> <button type="button">ADD Branch</button> </td> </tr> <tr> <td> <p> <b> Please select employee </b> </p> <select name="qv_employee"> <?php $args = array( 'post_type' => 'ag212_employee', 'post_status' => 'publish', 'posts_per_page' => 0, 'orderby' => 'title', 'order' => 'ASC', 'meta_query' =>array( array( 'key' => 'company_city', 'value' => 'limassol', 'compare' => '=', ), ), ); get posts from db, using args above $loop = new WP_Query( $args ); ?> <?php loop through available posts if they exist if($loop->have_posts()){ while ( $loop->have_posts() ) : $loop->the_post(); global $post; manipulate data in post <option value="[company_name]">[company_name]</option> echo '<option value="'.get_the_title($post->ID).'">'.$post->post_title.'</option>'; get_the_title( int|WP_Post $post ) endwhile; } else { echo '<option value="false">No employee found</option>'; } wp_reset_postdata(); ?> </select> <button type="button" color = "Green">ADD Employee</button> </td> </tr> <tr> <td> <p><b> Please Select your requried field. Test Or survey </b> </p> <select name="qv_quiz"> <option value="1">Quiz for Personal development</option> <option value="2">Quiz Motivational issue</option> <option value="3">Survey About us</option> <option value="4">Survey FOr Work improvement</option> </select> </td> </tr> <tr> <td> <input type="submit"/> </form> </td> </tr> </table> <table width= "100%" border = "10"> <tr> <td> <?php if(isset($_GET['qv_quiz'])){ $quiz_id_variable = $_GET['Color']; echo do_shortcode('[qsm quiz='.$_GET['qv_quiz'].']') ; } ?> </td> </tr> </table> <?php the_content(); ?> <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'photoline-lite' ), 'after' => '</div>', ) ); ?> </div><!-- .entry-content --> <?php edit_post_link( __( 'Edit', 'photoline-lite' ), '<footer class="entry-meta"><span class="edit-link">', '</span></footer>' ); ?> </article><!-- #post-## --> <?php endwhile; // end of the loop. ?> </main><!-- #main --> </div><!-- #primary --> <?php get_footer(); ?> 在末尾的注释中可重复显示,则将第一行作为标题并将列转换为数字。然后按照指示选择行:

DT

注意

DT <- fread(paste(paste(do.call("paste", DT), collapse = "\n")), na.strings = ".")

DT[apply(DT > 61, 1, any), ]
## Empty data.table (0 rows) of 4 cols: ETDPAT04,ETDPAT06,ETDPAT08,ETDPAT12