Telegram Group & Telegram Channel
Как разбить таблицу на части по заданному количеству строк

Иногда вам может потребоваться разбить таблицу на части, либо по количеству строк, либо по значениям какого то поля, в этом вам поможет функция split() из базового R.

Для начала построим тестовую таблицу:

# к-во строк в тестовой таблице
rows_in_table <- 570

# тестовая таблица
df <- data.frame(
row_num = 1:rows_in_table,
numbers = sample(1:9000, size = rows_in_table, replace = T),
letters = sample(letters, size = rows_in_table, replace = T)
)


Теперь разобьём таблицу на части по значению поля letters:

# Разбивка по значению какого либо поля
df_split_by_column <- split(df, df$letters)


$a
# A tibble: 28 x 3
row_num numbers letters
<int> <int> <chr>
1 3 8 a
2 11 2217 a
3 19 1948 a
4 34 338 a
5 54 604 a
6 64 754 a
7 68 3479 a
8 92 3942 a
9 160 7475 a
10 169 2507 a
# i 18 more rows
# i Use `print(n = ...)` to see more rows

$b
# A tibble: 20 x 3
row_num numbers letters
<int> <int> <chr>
1 9 4438 b
2 14 56 b
3 109 6039 b
4 159 1749 b
5 175 8068 b
6 200 7444 b
7 220 1101 b
8 234 2396 b
9 260 3112 b
10 296 7411 b
11 302 4639 b
12 354 7716 b
13 396 4090 b
14 398 1540 b
15 424 6738 b
16 426 728 b
17 441 333 b
18 503 3346 b
19 548 3347 b
20 564 651 b

$c
# A tibble: 17 x 3
row_num numbers letters
<int> <int> <chr>
1 47 1900 c
2 89 8522 c
3 130 6156 c
4 131 246 c
5 148 8591 c
6 151 640 c
7 154 8428 c
8 209 4218 c
9 216 7774 c
10 298 753 c
11 307 8839 c
12 355 5903 c
13 421 5147 c
14 433 2422 c
15 442 3224 c
16 557 4346 c
17 562 8635 c

...


Так же мы можем разбить таблицу на части по заданному количеству строк:

# Разбивка по заданному количеству строк
chunk <- 100 # размер одной части в к-ве строк
n <- nrow(df) # вычисляем к-во строк исходной таблицы
r <- rep(1:ceiling(n/chunk),each=chunk)[1:n] # определяем каждую строку в определённую часть таблицы
df_split_by_100_rows <- split(df,r) # Разбиваем таблицу


$`1`
row_num numbers letters
1 1 1210 h
2 2 5087 y
3 3 81 y
4 4 5459 a
5 5 5665 j
6 6 3735 v
7 7 4309 p
8 8 3858 i
9 9 847 x
10 10 91 e
11 11 3859 k

...


#заметки_по_R



group-telegram.com/R4marketing/1307
Create:
Last Update:

Как разбить таблицу на части по заданному количеству строк

Иногда вам может потребоваться разбить таблицу на части, либо по количеству строк, либо по значениям какого то поля, в этом вам поможет функция split() из базового R.

Для начала построим тестовую таблицу:

# к-во строк в тестовой таблице
rows_in_table <- 570

# тестовая таблица
df <- data.frame(
row_num = 1:rows_in_table,
numbers = sample(1:9000, size = rows_in_table, replace = T),
letters = sample(letters, size = rows_in_table, replace = T)
)


Теперь разобьём таблицу на части по значению поля letters:

# Разбивка по значению какого либо поля
df_split_by_column <- split(df, df$letters)


$a
# A tibble: 28 x 3
row_num numbers letters
<int> <int> <chr>
1 3 8 a
2 11 2217 a
3 19 1948 a
4 34 338 a
5 54 604 a
6 64 754 a
7 68 3479 a
8 92 3942 a
9 160 7475 a
10 169 2507 a
# i 18 more rows
# i Use `print(n = ...)` to see more rows

$b
# A tibble: 20 x 3
row_num numbers letters
<int> <int> <chr>
1 9 4438 b
2 14 56 b
3 109 6039 b
4 159 1749 b
5 175 8068 b
6 200 7444 b
7 220 1101 b
8 234 2396 b
9 260 3112 b
10 296 7411 b
11 302 4639 b
12 354 7716 b
13 396 4090 b
14 398 1540 b
15 424 6738 b
16 426 728 b
17 441 333 b
18 503 3346 b
19 548 3347 b
20 564 651 b

$c
# A tibble: 17 x 3
row_num numbers letters
<int> <int> <chr>
1 47 1900 c
2 89 8522 c
3 130 6156 c
4 131 246 c
5 148 8591 c
6 151 640 c
7 154 8428 c
8 209 4218 c
9 216 7774 c
10 298 753 c
11 307 8839 c
12 355 5903 c
13 421 5147 c
14 433 2422 c
15 442 3224 c
16 557 4346 c
17 562 8635 c

...


Так же мы можем разбить таблицу на части по заданному количеству строк:

# Разбивка по заданному количеству строк
chunk <- 100 # размер одной части в к-ве строк
n <- nrow(df) # вычисляем к-во строк исходной таблицы
r <- rep(1:ceiling(n/chunk),each=chunk)[1:n] # определяем каждую строку в определённую часть таблицы
df_split_by_100_rows <- split(df,r) # Разбиваем таблицу


$`1`
row_num numbers letters
1 1 1210 h
2 2 5087 y
3 3 81 y
4 4 5459 a
5 5 5665 j
6 6 3735 v
7 7 4309 p
8 8 3858 i
9 9 847 x
10 10 91 e
11 11 3859 k

...


#заметки_по_R

BY R4marketing | канал Алексея Селезнёва | Язык R




Share with your friend now:
group-telegram.com/R4marketing/1307

View MORE
Open in Telegram


Telegram | DID YOU KNOW?

Date: |

At the start of 2018, the company attempted to launch an Initial Coin Offering (ICO) which would enable it to enable payments (and earn the cash that comes from doing so). The initial signals were promising, especially given Telegram’s user base is already fairly crypto-savvy. It raised an initial tranche of cash – worth more than a billion dollars – to help develop the coin before opening sales to the public. Unfortunately, third-party sales of coins bought in those initial fundraising rounds raised the ire of the SEC, which brought the hammer down on the whole operation. In 2020, officials ordered Telegram to pay a fine of $18.5 million and hand back much of the cash that it had raised. Soloviev also promoted the channel in a post he shared on his own Telegram, which has 580,000 followers. The post recommended his viewers subscribe to "War on Fakes" in a time of fake news. Despite Telegram's origins, its approach to users' security has privacy advocates worried. In view of this, the regulator has cautioned investors not to rely on such investment tips / advice received through social media platforms. It has also said investors should exercise utmost caution while taking investment decisions while dealing in the securities market. "Markets were cheering this economic recovery and return to strong economic growth, but the cheers will turn to tears if the inflation outbreak pushes businesses and consumers to the brink of recession," he added.
from tw


Telegram R4marketing | канал Алексея Селезнёва | Язык R
FROM American