2021年4月28日星期三

Split dataframe into minimum n observation and do it for all rows in R

I have a question about dividing the dataset into several rows by function that we can set the number of minimal observations should contain in it.

Let say I have a data frame df with 8 observation:

df <- tibble(x = sort(sample(10:90, 8)), y = c("N","M", "N", "M", "N", "N", "M", "M"))    # A tibble: 8 x 2        x y        <int> <chr>  1    13 N      2    14 M      3    15 N      4    43 M      5    49 N      6    55 N      7    56 M      8    74 M     

I want to split it with n values, let say I want the n=3, the output must be a data frame with the minimal number of 3 rows and the other should 5. Next, it iterates, and appends by one row and stops until it finds the number of observations of both less than 3. I want to save this data frame in a separate df. Let say df_3 and dfo_5, df_4 and dfo_4

It maybe sounds confusing I will put the example of output that I suggested, let say I want the n=3

# rows df_3        x y        <int> <chr>  1    13 N      2    14 M      3    15 N      # dfo_5 = the remain df have 5 rows        x y        <int> <chr>  1    43 M      2    49 N      3    55 N      4    56 M      5    74 M    
# rows df_4        x y        <int> <chr>  1    13 N      2    14 M      3    15 N      4    43 M      # dfo_4 = the remain df have 4 rows        x y        <int> <chr>  1    49 N      2    55 N      3    56 M      4    74 M     
#   #   # rows df_6 is 6 ( i will not save this)        x y        <int> <chr>  1    13 N      2    14 M      3    15 N      4    43 M      5    49 N      6    55 N       # because the remainder df contains less 3 rows        x y        <int> <chr>  1    56 M      2    74 M          

A help will be much appreciated. Thank You

https://stackoverflow.com/questions/67310903/split-dataframe-into-minimum-n-observation-and-do-it-for-all-rows-in-r April 29, 2021 at 12:06PM

没有评论:

发表评论