Reputation: 1946
My data is structured as follows:
Name Drill Movement Repetition DV
1 RUTH 90_Turn Sprint 1 10
2 RUTH 90_Turn Sprint 1 12
2 RUTH 90_Turn Sprint 2 12
2 RUTH 90_Turn Sprint 2 9
3 RUTH 90_Turn Sprint 3 14
3 RUTH 90_Turn Sprint 3 12
4 RUTH 90_Turn Walk 1 13
4 RUTH 90_Turn Walk 1 17
5 RUTH 90_Turn Walk 2 11
5 RUTH 90_Turn Walk 2 15
I would like to add in a column Trial
that contains a code for each unique combination of Movement
and Repetition
, such as:
Name Drill Movement Repetition DV Trial
1 RUTH 90_Turn Sprint 1 10 D90_Sprint1
2 RUTH 90_Turn Sprint 1 12 D90_Sprint1
2 RUTH 90_Turn Sprint 2 12 D90_Sprint2
2 RUTH 90_Turn Sprint 2 9 D90_Sprint2
3 RUTH 90_Turn Sprint 3 14 D90_Sprint3
3 RUTH 90_Turn Sprint 3 12 D90_Sprint3
4 RUTH 90_Turn Walk 1 13 D90_Walk1
4 RUTH 90_Turn Walk 1 17 D90_Walk1
5 RUTH 90_Turn Walk 2 11 D90_Walk2
5 RUTH 90_Turn Walk 2 15 D90_Walk2
This takes into account the Drill
which remains constant, along with Name
- the data.frame
only consists of Ruth's data for this drill. The DV
is measured at least twice per Movement
and Repetition
.
Is it possible to do this?
My data frame is 10140 obs. so a quick solution would be ideal. Thank you!
Upvotes: 0
Views: 80
Reputation: 887213
We can use paste
df1$Trial <- paste0("D90_", df1$Movement, df1$Repetition)
If '90' comes from the 'Drill' column
df1$Trial <- paste0("D", sub("_.*", "", df1$Drill), "_", df1$Movement, df1$Repetition)
Or with sprintf
sprintf("D%s_%s%d", sub("_.*", "", df1$Drill), df1$Movement, df1$Repetition)
#[1] "D90_Sprint1" "D90_Sprint1" "D90_Sprint2" "D90_Sprint2" "D90_Sprint3" "D90_Sprint3" "D90_Walk1" "D90_Walk1" "D90_Walk2"
#[10] "D90_Walk2"
Upvotes: 1
Reputation: 521457
Use paste0()
:
df$Trial <- paste0(sub("_.*", "", df$Drill),
"_",
df$Movement,
df$Repetition)
The call to sub()
extracts the Drill
component of the final Trial
string.
Upvotes: 1