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



我有以下数据帧(这是示例(:

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_inmpl(.data,dots(中的错误:参数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          

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

我的df数据帧被缩放并居中-生成df的函数返回:

scale(df)

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

当转换为data.table时,它解决了问题:

df %>% as.data.table() %>% dplyr::arrange(obs_id, user_id, scroll_id, timestamp)

如果我错了,请纠正我。

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 

最新更新