[英]How to create new column from combination of existing character and numeric columns?
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: 我想在
Trial
栏中添加一个代码,其中包含针对Movement
和Repetition
每个唯一组合的代码,例如:
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. 这考虑了保持不变的
Drill
以及Name
data.frame
仅包含该钻取的Ruth数据。 The DV
is measured at least twice per Movement
and Repetition
. 每次
Movement
和Repetition
至少测量两次DV
。
Is it possible to do this? 是否有可能做到这一点?
My data frame is 10140 obs. 我的数据框是10140 obs。 so a quick solution would be ideal.
因此快速解决方案将是理想的。 Thank you!
谢谢!
We can use paste
我们可以用
paste
df1$Trial <- paste0("D90_", df1$Movement, df1$Repetition)
If '90' comes from the 'Drill' column 如果“ 90”来自“钻取”列
df1$Trial <- paste0("D", sub("_.*", "", df1$Drill), "_", df1$Movement, df1$Repetition)
Or with sprintf
或与
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"
Use paste0()
: 使用
paste0()
:
df$Trial <- paste0(sub("_.*", "", df$Drill),
"_",
df$Movement,
df$Repetition)
The call to sub()
extracts the Drill
component of the final Trial
string. 对
sub()
的调用提取了最终Trial
字符串的Drill
组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.