按顺序创建重复值序列?

发布于 2024-11-16 10:27:49 字数 461 浏览 3 评论 0原文

我需要一系列重复的数字,即 1 1 ... 1 2 2 ... 2 3 3 ... 3 等 我实现这个的方法是:

  nyear <- 20
  names <- c(rep(1,nyear),rep(2,nyear),rep(3,nyear),rep(4,nyear),
             rep(5,nyear),rep(6,nyear),rep(7,nyear),rep(8,nyear))

它有效,但很笨拙,并且显然不能很好地扩展。

如何按顺序重复 N 个整数 M 次?

  • 我尝试嵌套 seq()rep() 但这并不完全做我想做的事。
  • 显然我可以编写一个 for 循环来执行此操作,但应该有一种内在的方法来执行此操作!

I need a sequence of repeated numbers, i.e. 1 1 ... 1 2 2 ... 2 3 3 ... 3 etc. The way I implemented this was:

  nyear <- 20
  names <- c(rep(1,nyear),rep(2,nyear),rep(3,nyear),rep(4,nyear),
             rep(5,nyear),rep(6,nyear),rep(7,nyear),rep(8,nyear))

which works, but is clumsy, and obviously doesn't scale well.

How do I repeat the N integers M times each in sequence?

  • I tried nesting seq() and rep() but that didn't quite do what I wanted.
  • I can obviously write a for-loop to do this, but there should be an intrinsic way to do this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒岛晴空 2024-11-23 10:27:49

您错过了 rep()each= 参数:

R> n <- 3
R> rep(1:5, each=n)
 [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
R> 

因此您的示例可以通过简单的方法来完成

R> rep(1:8, each=20)

You missed the each= argument to rep():

R> n <- 3
R> rep(1:5, each=n)
 [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
R> 

so your example can be done with a simple

R> rep(1:8, each=20)
空城之時有危險 2024-11-23 10:27:49

另一个base R选项可以是gl()

gl(5, 3)

其中输出是一个因子:

 [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
Levels: 1 2 3 4 5

如果需要整数,您可以将其转换:

as.numeric(gl(5, 3))

 [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5

Another base R option could be gl():

gl(5, 3)

Where the output is a factor:

 [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
Levels: 1 2 3 4 5

If integers are needed, you can convert it:

as.numeric(gl(5, 3))

 [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
離人涙 2024-11-23 10:27:49

对于你的例子,德克的答案是完美的。如果您有一个数据框并希望将这种序列添加为一列,您还可以使用 groupdata2 (免责声明:我的包)中的 group 将数据点贪婪地分成组。

# Attach groupdata2
library(groupdata2)
# Create a random data frame
df <- data.frame("x" = rnorm(27))
# Create groups with 5 members each (except last group)
group(df, n = 5, method = "greedy")
         x .groups
     <dbl> <fct>  
 1  0.891  1      
 2 -1.13   1      
 3 -0.500  1      
 4 -1.12   1      
 5 -0.0187 1      
 6  0.420  2      
 7 -0.449  2      
 8  0.365  2      
 9  0.526  2      
10  0.466  2      
# … with 17 more rows

创建这种分组因素的方法有很多种。例如,按组数、组大小列表,或者当某列中的值与前一行中的值不同时让组开始(例如,如果列是 c("x","x", "y","z","z") 分组因子将为 c(1,1,2,3,3)

For your example, Dirk's answer is perfect. If you instead had a data frame and wanted to add that sort of sequence as a column, you could also use group from groupdata2 (disclaimer: my package) to greedily divide the datapoints into groups.

# Attach groupdata2
library(groupdata2)
# Create a random data frame
df <- data.frame("x" = rnorm(27))
# Create groups with 5 members each (except last group)
group(df, n = 5, method = "greedy")
         x .groups
     <dbl> <fct>  
 1  0.891  1      
 2 -1.13   1      
 3 -0.500  1      
 4 -1.12   1      
 5 -0.0187 1      
 6  0.420  2      
 7 -0.449  2      
 8  0.365  2      
 9  0.526  2      
10  0.466  2      
# … with 17 more rows

There's a whole range of methods for creating this kind of grouping factor. E.g. by number of groups, a list of group sizes, or by having groups start when the value in some column differs from the value in the previous row (e.g. if a column is c("x","x","y","z","z") the grouping factor would be c(1,1,2,3,3).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文