将数据框分解为较小的数据框并保存

时间:2018-11-19 14:28:54

标签: r dataframe

需要帮助根据列间隔将一个数据帧动态拆分为多个较小的数据帧,并保存。 示例:

x = data.frame(num = 1:26, let = letters, LET = LETTERS)

上述数据框 x 需要基于 num 中的值,以 5 的间隔分为较小的数据框。 结果将是6个数据帧

> 1.    0 – 5
> 2.    6 – 10
> 3.    11 – 15
> 4.    16 -20
> 5.    21 -25
> 6.    26 – 30

谢谢你, 维克拉姆

4 个答案:

答案 0 :(得分:5)

您可以使用static mut I: i64 = 5; static mut J: i64 = unsafe { I } + 3; fn main() { unsafe { println!("I: {}, J: {}", I, J); } } 函数和split函数来执行操作:

cut

然后您可以将此列表传递到x = data.frame(num = 1:26, let = letters, LET = LETTERS) answer<-split(x, cut(x$num, breaks=c(0, 5, 10, 15, 20, 25, 30))) 进行进一步处理。

答案 1 :(得分:2)

使用tidyverse

library(tidyverse)

x = data.frame(num = 1:26, let = letters, LET = LETTERS)


##Brake the data frame
y <- x %>%
  mutate(group = cut_width(num,5, boundary = 0,closed = "right"))

##Put them into a list
list_1 <- lapply(1:length(unique(y$group)),
                function(i)filter(y, group == unique(y$group)[i]))

答案 2 :(得分:2)

还考虑将记录标记为5的倍数,然后运行by,该功能将数据帧按一个或多个因素进行分割:

df <- data.frame(num = 1:26, let = letters, LET = LETTERS)

df$grp <- ceiling(df$num / 5)

df_list <- by(df, df$grp, function(sub) transform(sub, grp=NULL))

输出

df_list

# df$grp: 1
#   num let LET
# 1   1   a   A
# 2   2   b   B
# 3   3   c   C
# 4   4   d   D
# 5   5   e   E
# ------------------------------------------------------------------------------------------- 
# df$grp: 2
#    num let LET
# 6    6   f   F
# 7    7   g   G
# 8    8   h   H
# 9    9   i   I
# 10  10   j   J
# ------------------------------------------------------------------------------------------- 
# df$grp: 3
#    num let LET
# 11  11   k   K
# 12  12   l   L
# 13  13   m   M
# 14  14   n   N
# 15  15   o   O
# ------------------------------------------------------------------------------------------- 
# df$grp: 4
#    num let LET
# 16  16   p   P
# 17  17   q   Q
# 18  18   r   R
# 19  19   s   S
# 20  20   t   T
# ------------------------------------------------------------------------------------------- 
# df$grp: 5
#    num let LET
# 21  21   u   U
# 22  22   v   V
# 23  23   w   W
# 24  24   x   X
# 25  25   y   Y
# ------------------------------------------------------------------------------------------- 
# df$grp: 6
#    num let LET
# 26  26   z   Z

答案 3 :(得分:2)

这似乎是一种更整洁的方式。您可以轻松调整输出文件的名称和分割数

library(tidyverse)

df <- data.frame(num = 1:26, let = letters, LET = LETTERS)

# split data frame into 6 pieces
split_df <- split(df, ceiling(1:nrow(df) / nrow(df) * 6))

# save each of them in turn
split_df %>%
 names(.) %>%
 walk(~ write_csv(split_df[[.]], paste0("part_", ., ".csv")))