【问题标题】:How to print certain rows of a dataframe with a condition如何使用条件打印数据框的某些行
【发布时间】:2017-06-02 20:29:22
【问题描述】:

从下面的 data.frame 中,我想使用 print() 函数简单地打印出任何具有“红色”警告变量的行的行信息(日期和温度)。我尝试过索引,但没有运气!蒂亚

              date  temp warnings
            <dttm> <dbl>    <chr>
 1 2017-04-16 10:00:00  26.3    black
 2 2017-04-17 10:00:00  25.7    black
 3 2017-04-18 10:00:00  25.4    black
 4 2017-04-19 10:00:00  25.6    black
 5 2017-04-20 10:00:00  25.9    black
 6 2017-04-21 10:00:00  26.1    black
 7 2017-04-22 10:00:00  27.8    red
 8 2017-04-23 10:00:00  26.3    black
 9 2017-04-24 10:00:00  26.1    black
10 2017-04-25 10:00:00  26.6    black

谢谢大家!我玩弄了你的建议并设法打印,但有没有办法把它整理好而不打印列标题?

df[df$warnings == 'red', c('date', 'temp')]
# A tibble: 2 x 2
                 date  temp
               <dttm> <dbl>
1 2017-05-27 10:00:00  27.8
2 2017-05-29 10:00:00  27.7

【问题讨论】:

  • 索引应该可以工作...您能提供一个数据示例吗?
  • 我对 r 很陌生,所以您必须原谅我的理解不足...您希望以哪些数据为例?我附加到问题的 data.frame 样本是否可见?否则我有一个巨大的 data.frame 也有更多的变量,但真的只需要在温度大于 27 时打印,相应的时间是多少?
  • 只是您粘贴的示例。你能在这里 dput() 吗?
  • df[df$warnings == 'red', c('date', 'temp')] 或 df[df$warnings == 'red', 1:2] 应该这样做。
  • 谢谢大家!最后一个问题请参阅编辑后的帖子:)

标签: r indexing printing


【解决方案1】:

我使用包“dplyr”来完成类似的任务。

install.packages(dplyr)
library(dplyr)
filter(my_df, warnings =='red')

其中 my_df 是您的表的名称。

【讨论】:

    【解决方案2】:
    > library("readr")
    > red2=read_csv("red.csv")
    Parsed with column specification:
    cols(
      date = col_character(),
      temp = col_double(),
      warning = col_character()
    )
    > red2
    # A tibble: 10 x 3
                  date  temp warning
                 <chr> <dbl>   <chr>
     1 4/16/2017 10:00  26.3   black
     2 4/17/2017 10:00  25.7   black
     3 4/18/2017 10:00  25.4   black
     4 4/19/2017 10:00  25.6   black
     5 4/20/2017 10:00  25.9   black
     6 4/21/2017 10:00  26.1   black
     7 4/22/2017 10:00  27.8     red
     8 4/23/2017 10:00  26.3   black
     9 4/24/2017 10:00  26.1   black
    10 4/25/2017 10:00  26.6   black
    > library(lubridate)
    
    Attaching package: ‘lubridate’
    
    The following object is masked from ‘package:base’:
    
        date
    
    > red2$date=mdy_hm(red2$date)
    > red2
    # A tibble: 10 x 3
                      date  temp warning
                    <dttm> <dbl>   <chr>
     1 2017-04-16 10:00:00  26.3   black
     2 2017-04-17 10:00:00  25.7   black
     3 2017-04-18 10:00:00  25.4   black
     4 2017-04-19 10:00:00  25.6   black
     5 2017-04-20 10:00:00  25.9   black
     6 2017-04-21 10:00:00  26.1   black
     7 2017-04-22 10:00:00  27.8     red
     8 2017-04-23 10:00:00  26.3   black
     9 2017-04-24 10:00:00  26.1   black
    10 2017-04-25 10:00:00  26.6   black
    

    只选择那些有红色警告的行,按列号只选择两列

    >     red2[red2$warning=="red",1:2]
    # A tibble: 1 x 2
                     date  temp
                   <dttm> <dbl>
    1 2017-04-22 10:00:00  27.8
    

    现在为了解决大数据的问题,我把数据模拟得更大

    > red3=red2[rep(seq_len(nrow(red2)), each=2000000),]
    > str(red3)
    Classes ‘tbl_df’, ‘tbl’ and 'data.frame':       20000000 obs. of  3 variables:
     $ date   : POSIXct, format: "2017-04-16 10:00:00" "2017-04-16 10:00:00" ...
     $ temp   : num  26.3 26.3 26.3 26.3 26.3 26.3 26.3 26.3 26.3 26.3 ...
     $ warning: chr  "black" "black" "black" "black" ...
    > system.time(red3[red3$warning=="red",1:2])
       user  system elapsed 
       0.17    0.06    0.24 
    

    现在为了打印没有列标题的数据,我们使用来自How do you delete the header in a dataframe? 的带有 dimnames =NULL 的矩阵

     > out=red2[red2$warning=="red",1:2]> out.print <- function(dat) print(matrix(as.matrix(out),ncol=ncol(out),dimnames=NULL),quote=F)
    > out.print(out)
         [,1]                [,2]
    [1,] 2017-04-22 10:00:00 27.8
    

    【讨论】:

    • 不清楚为什么 OPs 索引不起作用;也不是以下 的含义>。你有什么想法吗?
    • 当您在 Rstudio 中由 readr 导入时,您会得到这个 - 似乎是变量类型
    • 当您在 Rstudio 中通过 readr 导入时,您会发现 - 似乎是变量类型, 是双精度或数字, 是字符, 是日期。
    猜你喜欢
    • 2021-11-28
    • 2021-04-22
    • 2021-01-20
    • 2019-09-23
    • 1970-01-01
    • 2020-10-07
    • 2011-09-26
    • 1970-01-01
    • 2020-10-01
    相关资源
    最近更新 更多