R 在列中创建重复模式 [英] R create repeating pattern in column

查看:48
本文介绍了R 在列中创建重复模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列长度为 1536 的列,其中包含从 1 到 1536 的每小时计数".我只想为包含 1:7 的天"创建第二列,每个列重复 24 次,用于整个向量长度.

I have a column of length 1536 that contains an hourly 'count' from 1 to 1536. I JUST want to create a second column for 'day' that contains 1:7, each repeated 24 times, for the entire vector length.

Ldata$day <- rep(1:7, each=24)

Ldata$day <- rep(1:7, each=24)

上面的代码应该可以工作,但是该死的东西一直在抛出错误'替换有 168 行,数据有 1536'为什么这段代码不能只是尽可能多地填充向量然后停止?为什么这么难/为什么甚至需要抛出错误.只需将序列放入列中.....

The above code SHOULD work, but the damn thing keeps throwing an error of 'replacement has 168 rows, data has 1536' Why can't this code just fill in the vector as much as possible then stop? Why is that so hard/why is throwing an error even needed. Just put the sequence into the column.....

推荐答案

您可以同时指定 rep() 的 'each' 和 'length.out' 参数:

You can specify both the 'each' and 'length.out' parameters of rep():

Ldata$day <- rep(1:7, each=24, length.out=1536)

这篇关于R 在列中创建重复模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆