> 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
現在打印出沒有列標題的數據,我們使用矩陣與dimnames = NULL從How do you delete the header in a dataframe?
> 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
索引應該工作...你能提供一個你的數據的例子嗎? – user1945827
我對r很新,所以你不得不原諒我缺乏理解......你喜歡哪個數據的例子?我附加到問題的data.frame示例是否可見?否則,我有一個巨大的data.frame,並且有更多的變量,但是當temp大於27並且相應的時間是什麼時,真的只需要打印? – LauraKate
只是你粘貼的樣本。你可以在這裏輸入()嗎? – user1945827