dplyr :: arrange拒绝按时间戳列排列数据帧?

时间:2018-11-08 09:09:25

标签: r dataframe dplyr tidyverse

我有以下数据框(以下是示例):

df <- structure(list(user_id = c(1L, 1L, 1L, 1L, 1L, 1L), obs_id = c(1L, 
2L, 2L, 2L, 2L, 2L), scroll_id = c(3L, 1L, 2L, 3L, 4L, 5L), timestamp = c(-1.74966971796047, 
-1.70403832189443, -1.70379906928687, -1.70361867040459, -1.70347088963619, 
-1.70319128699835), row_num = 1:6, scroll_length = c(6, 9, 14, 
12, 13, 26), x_mean = c(-1.74134749014902, -1.19087086808828, 
1.36178725012622, -1.32786301490502, 1.24184201608646, -1.31953110973881
), y_mean = c(-4.93507461932646, 0.0304680987883223, 0.140001980341645, 
0.61911843405746, 0.434230282460559, 0.438563278736709), dx_mean = c(-0.514034686928457, 
-0.709482080612108, 0.924636289935977, -0.702980646737082, 0.515080876392673, 
-0.359676884238743), dy_mean = c(0.972265996197407, -0.692113718739584, 
-0.162463490249733, -0.373682612876388, -0.0663766957581004, 
0.293619375985922)), .Names = c("user_id", "obs_id", "scroll_id", 
"timestamp", "row_num", "scroll_length", "x_mean", "y_mean", 
"dx_mean", "dy_mean"), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

我想按“时间戳”列进行排列,但出现以下错误:

data %>% arrange(timestamp)
data %>% arrange("timestamp")
  

arrange_impl(.data,点)中的错误:参数1是不受支持的类型   矩阵

请告知如何使其工作。我知道时间戳是一个函数和矩阵,但是这里是一列,我“想” dplyr“理解”这是一列。

@sotos询问:

sessionInfo():

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_IL.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_IL.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_IL.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_IL.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2.2      rebus_0.1-3         philentropy_0.2.0   reshape2_1.4.3      broom_0.5.0         dummies_1.5.6       hms_0.4.2          
 [8] anytime_0.3.1       data.table_1.11.8   bit64_0.9-7         bit_1.1-14          car_3.0-2           carData_3.0-2       caret_6.0-80       
[15] lattice_0.20-35     xgboost_0.71.2      doSNOW_1.0.16       snow_0.4-3          doMC_1.3.5          iterators_1.0.10    foreach_1.4.4      
[22] randomForest_4.6-14 htmlwidgets_1.3     plotly_4.8.0        jsonlite_1.5        pROC_1.13.0         knitr_1.20          lubridate_1.7.4    
[29] MASS_7.3-49         chron_2.3-53        forcats_0.3.0       stringr_1.3.1       dplyr_0.7.7         purrr_0.2.5         readr_1.1.1        
[36] tidyr_0.8.2         tibble_1.4.2        ggplot2_3.1.0       tidyverse_1.2.1    

loaded via a namespace (and not attached):
 [1] nlme_3.1-137          dimRed_0.1.0          httr_1.3.1            tools_3.4.4           backports_1.1.2       R6_2.3.0             
 [7] rpart_4.1-13          rebus.base_0.0-3      lazyeval_0.2.1        colorspace_1.3-2      nnet_7.3-12           withr_2.1.2          
[13] tidyselect_0.2.5      curl_3.1              compiler_3.4.4        cli_1.0.1             rvest_0.3.2           xml2_1.2.0           
[19] scales_1.0.0          sfsmisc_1.1-2         DEoptimR_1.0-8        robustbase_0.93-3     RApiDatetime_0.0.4    digest_0.6.18        
[25] rebus.unicode_0.0-2   foreign_0.8-70        rio_0.5.10            pkgconfig_2.0.2       htmltools_0.3.6       rlang_0.3.0.1        
[31] readxl_1.1.0          ddalpha_1.3.4         rstudioapi_0.8        bindr_0.1.1           zip_1.0.0             ModelMetrics_1.2.0   
[37] magrittr_1.5          Matrix_1.2-14         Rcpp_0.12.19          munsell_0.5.0         abind_1.4-5           stringi_1.2.4        
[43] yaml_2.2.0            plyr_1.8.4            recipes_0.1.3         grid_3.4.4            pls_2.7-0             crayon_1.3.4         
[49] rebus.datetimes_0.0-1 haven_1.1.2           splines_3.4.4         pillar_1.3.0          rebus.numbers_0.0-1   codetools_0.2-15     
[55] stats4_3.4.4          CVST_0.2-2            magic_1.5-9           glue_1.3.0            modelr_0.1.2          cellranger_1.1.0     
[61] gtable_0.2.0          kernlab_0.9-27        assertthat_0.2.0      DRR_0.0.3             openxlsx_4.1.0        gower_0.1.2          
[67] prodlim_2018.04.18    class_7.3-14          survival_2.42-3       viridisLite_0.3.0     geometry_0.3-6        timeDate_3043.102    
[73] RcppRoll_0.3.0        lava_1.6.3            ipred_0.9-7          

2 个答案:

答案 0 :(得分:1)

df %>% arrange(timestamp)

arrange() from dplyr arranges as below in ascending order
# A tibble: 6 x 10
  user_id obs_id scroll_id timestamp row_num scroll_length x_mean  y_mean dx_mean dy_mean
    <int>  <int>     <int>     <dbl>   <int>         <dbl>  <dbl>   <dbl>   <dbl>   <dbl>
1       1      1         3     -1.75       1             6  -1.74 -4.94    -0.514  0.972 
2       1      2         1     -1.70       2             9  -1.19  0.0305  -0.709 -0.692 
3       1      2         2     -1.70       3            14   1.36  0.140    0.925 -0.162 
4       1      2         3     -1.70       4            12  -1.33  0.619   -0.703 -0.374 
5       1      2         4     -1.70       5            13   1.24  0.434    0.515 -0.0664
6       1      2         5     -1.70       6            26  -1.32  0.439   -0.360  0.294 

答案 1 :(得分:1)

感谢所有评论,我找到了一个解决方案:

我的public bool ConnectGoogle() { try { HttpURLConnection urlc = (HttpURLConnection)(new URL("http://www.google.com").OpenConnection()); urlc.SetRequestProperty("User-Agent", "Test"); urlc.SetRequestProperty("Connection", "close"); urlc.ConnectTimeout = 10000; urlc.Connect(); return (urlc.ResponseCode == HttpStatus.Accepted); } catch (Exception ex) { //Log(ex.Message); return false; } } 数据帧已缩放并居中-产生df的函数返回:

df

当我打印scale(df) 时,我看到的属性表明它居中并缩放。

转换为str(df)时,它解决了该问题:

data.table

如果我错了,请纠正我。