更新时间:2023-11-18 23:27:10
您可以将 split
与 cut
结合起来,在一行代码中完成,避免了需要为不同的数据范围使用一堆不同的表达式进行子集:
You can combine split
with cut
to do this in a single line of code, avoiding the need to subset with a bunch of different expressions for different data ranges:
split(dat, cut(dat$Age, c(0, 5, 10, 15), include.lowest=TRUE))
# $`[0,5]`
# Users Age
# 1 1 2
# 4 4 3
#
# $`(5,10]`
# Users Age
# 2 2 7
# 3 3 10
# 5 5 8
#
# $`(10,15]`
# [1] Users Age
# <0 rows> (or 0-length row.names)
cut
根据指定的断点拆分数据,split
根据提供的类别拆分数据框.如果将此计算的结果存储到名为 l
的列表中,则可以使用 l[[1]]
, l[[2] 访问较小的数据帧]]
和 l[[3]]
或更详细的:
cut
splits up data based on the specified break points, and split
splits up a data frame based on the provided categories. If you stored the result of this computation into a list called l
, you could access the smaller data frames with l[[1]]
, l[[2]]
, and l[[3]]
or the more verbose:
l$`[0,5]`
l$`(5,10]`
l$`(10, 15]`