r - 添加重复序列号的列
问题描述
我想将重复数字 1 到 577 的列添加到超过 15,000 行的数据框中。
这是数据框:
> head(corr_dat_cond1)
participant search_difficulty key_resp.corr key_resp.rt target_position distractor1_colour
1 1010 difficult 1 1.0820000 left [0.82,0.31,0]
2 1010 no_search 1 0.5400000 left [-1,-1,-1]
3 1010 difficult 1 0.5119998 down [0.82,0,0.31]
4 1010 no_search 1 0.7079999 right [-1,-1,-1]
5 1010 difficult 1 1.0249999 up [0.82,0.31,0]
6 1010 no_search 1 0.4889998 left [-1,-1,-1]
distractor2_colour non_target_colour non_target_pos cue_uposition target_char non_target_char cue_time
1 [0.82,0,0.31] [0.82,0.31,0] [0.328,0] up = x 1.1
2 [-1,-1,-1] [-1,-1,-1] [0.328,0] right x = 1.2
3 [0.82,0.31,0] [0.82,0,0.31] [0.328,0] down x = 1.0
4 [-1,-1,-1] [-1,-1,-1] [0,0.328] left = x 1.4
5 [0.82,0,0.31] [0.82,0.31,0] [0,-0.328] left x = 1.4
6 [-1,-1,-1] [-1,-1,-1] [0,-0.328] up x = 1.0
cue_colour n cue_validity mrt stdev low_cutoff high_cutoff cond trial_num
1 Mismatch (Onset) cue 577 FALSE 0.7639095 0.2481090 0.0195825 1.5082365 1 1
2 Mismatch (Onset) cue 577 FALSE 0.5530880 0.1243826 0.1799402 0.9262358 1 2
3 Mismatch (Onset) cue 577 TRUE 0.7639095 0.2481090 0.0195825 1.5082365 1 3
4 Match (Color) cue 577 FALSE 0.5530880 0.1243826 0.1799402 0.9262358 1 4
5 Match (Color) cue 577 FALSE 0.7639095 0.2481090 0.0195825 1.5082365 1 5
6 Mismatch (Onset) cue 577 FALSE 0.5530880 0.1243826 0.1799402 0.9262358 1 6
trial_num 列是我最初尝试添加一列序列号的尝试。这是我使用的代码:
corr_dat_cond1$trial_num <- 1:nrow(corr_dat_cond1)
但是,我希望这些数字每 577 行重复一次,而不是一直计数到数据框中的行数。
任何帮助,将不胜感激!谢谢你。
解决方案
您可以使用该rep_len
功能。
trial_num <- rep_len(1:577, nrow(corr_dat_cond1))
rep
这与使用length.out
指定的调用相同。
推荐阅读
- tkinter - 当按钮有命令时如何导入用 tkinter 编写的模块
- node.js - NodeJs - 字符串加密/解密的代码错误
- c# - 断点不通过委托 C# Visual Studio (UNITY)
- kubernetes - minikube 随机停止,无法运行 kubectl 命令
- android - SurfaceView 与 Android 8.0 上的另一个 SurfaceView 重叠
- symfony - Symfony/messenger - AMQP - 如何设置 content_type:application/json?
- scala - Spray Json:从通用对象中获取 Json 字符串
- python - 如何根据状态从 AWS 控制台查询图像 AMI:使用 Python boto3 可用?
- python - 加速 Pandas .apply 函数。计算行数并对其进行操作
- javascript - 查找正则表达式组