r-添加序列号重复序列的列



我想在超过15000行的数据帧中添加一个重复数字1到577的列。

这是数据帧:

> 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))

这与使用指定的length.out调用rep相同。

最新更新