我想知道两列相同的运行长度。例如,使用以下数据:
v1 v2
1 1
1 1
1 2
1 3
2 3
2 4
2 4
我想要的东西会返回(2,1,1,1,2)类似于rle函数对单个列的影响。是否有一个简单的函数可以做到这一点(或者rle函数也可以用来处理这种情况)?
答案 0 :(得分:5)
我们可以R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] DT_0.2 rgdal_1.1-10 sp_1.2-3 googlesheets_0.2.1 leaflet_1.0.1 shiny_0.14.2
loaded via a namespace (and not attached):
[1] Rcpp_0.12.7 xml2_1.0.0 magrittr_1.5 xtable_1.8-2 lattice_0.20-34 R6_2.2.0 stringr_1.1.0 httr_1.2.1 dplyr_0.5.0
[10] tools_3.3.2 grid_3.3.2 DBI_0.5-1 htmltools_0.3.5 yaml_2.1.14 lazyeval_0.2.0 openssl_0.9.5 assertthat_0.1 digest_0.6.10
[19] tibble_1.2 RJSONIO_1.3-0 readr_1.0.0 purrr_0.2.2 bitops_1.0-6 htmlwidgets_0.8 RCurl_1.95-4.8 rsconnect_0.4.3 curl_2.2
[28] mime_0.5 stringi_1.1.2 cellranger_1.1.0 jsonlite_1.1 httpuv_1.3.3
将这些列放在一起,应用paste
并获取rle
lengths
或rle(do.call(paste0, df1))$lengths
#[1] 2 1 1 1 2
data.table
或者更好的方法是library(data.table)
setDT(df1)[, .N, .(v1, v2)]$N
#[1] 2 1 1 1 2
rleid
data.table