• R语言
  • 请问如何用R语言,对一列数按照从上到下,元素和大致相等,分成十份

dapengde >
sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)China.936
[2] LC_CTYPE=Chinese (Simplified)
China.936

[3] LC_MONETARY=Chinese (Simplified)China.936
[4] LC_NUMERIC=C

[5] LC_TIME=Chinese (Simplified)
China.936

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

[7] base

other attached packages:
[1] sp_1.3-1

loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 compiler_3.5.1 pillar_1.3.0

[4] later_0.7.5 plyr_1.8.4 bindr_0.1.1

[7] tools_3.5.1 digest_0.6.18 tibble_1.4.2

[10] gtable_0.2.0 lattice_0.20-35 pkgconfig_2.0.2

[13] rlang_0.3.0.1 shiny_1.2.0 rstudioapi_0.8

[16] crosstalk_1.0.0 bindrcpp_0.2.2 dplyr_0.7.8

[19] htmlwidgets_1.3 leaflet_2.0.2 grid_3.5.1

[22] tidyselect_0.2.5 glue_1.3.0 R6_2.3.0

[25] ggplot2_3.1.0 purrr_0.2.5 magrittr_1.5

[28] scales_1.0.0 promises_1.0.1 htmltools_0.3.6

[31] rsconnect_0.4.1.4 assertthat_0.2.0 mime_0.6

[34] xtable_1.8-3 colorspace_1.3-2 httpuv_1.4.5.1

[37] lazyeval_0.2.1 munsell_0.5.0 crayon_1.3.4

    13520982976 建议你真的多看两眼左上角的新手须知,把自己的发帖整的清楚漂亮些,这样可以减少对话次数,提高效率,节约彼此的时间

      Cloud2016 请问如何指定某列呢?因为我的数据有很多列,需要根据不同列分组

      dapengde 您好,如果我有多个列,想按照不同的列进行分组,那么如何指定某个列呢,您给的答案特别好,就是没有指定某个列,所以希望您能给一些提示。

        6 天 后

        dapengde 你好,你的代码好像运行不了,而且给的第一个代码好像没有逻辑,不知讲的是什么,希望您能多指教,我希望就是按某一列原来的顺序,分成四份,第一份的元素和基本相等。

        dapengde 你的代码运行的结果不是我期望的,如果分成三份,两份,那么他们的元素和会基本一样吗,显然,你的运算是按我的结果算的,并不是按要求进行的。

          tctcab

          a <- c(2,3,1,4,2,2)
          
          
          # 把这组数分成三份
          # 每一份的元素和基本相等
          # 想要的结果是 b<- c(1,1,2,2,3,3)
          # 因为前两个和中间两个和后三个的和基本相等
          
          
          a <- c(2,3,1,4,2,2)
          a1 <- cumsum(a)
          a1
          n=3
          for(z in 1:n){
            for(i in 1:length(a1)){
              if(a1[i] <= a1[which(abs(a1-sum(a)/n)==min(abs(a1-sum(a)/n)))]*z)print(z)
            }
          }

          这是我试着写的,有一些错误,希望您能帮个忙

            13520982976

            使用cumsum(a)/n的思路大方向是对的,试试这个:

            library(tidyverse)
            
            # data
            set.seed(123)
            a = c(2,3,1,4,2,2)
            
            # number of groups
            n =3
            
            # divide a into n groups using cut
            
            grp = as.numeric(cut(cumsum(a), n))
            
            # result
            df = data.frame(a=a, grp = grp)
            
            result.df = df %>%
              group_by(grp) %>%
              mutate(grpsum = sum(a))
            
            result.df
            #> # A tibble: 6 x 3
            #> # Groups:   grp [3]
            #>       a   grp grpsum
            #>   <dbl> <dbl>  <dbl>
            #> 1     2     1      6
            #> 2     3     1      6
            #> 3     1     1      6
            #> 4     4     2      4
            #> 5     2     3      4
            #> 6     2     3      4

            <sup>Created on 2019-03-05 by the reprex package (v0.2.1)</sup>

            你需要的分组用 grp = as.numeric(cut(cumsum(a), n))应该够了

            不过这个分组跟你的112233稍微不一样,在我测试100个元素时,肉眼看了一下每组的和还是大致均匀。