问题描述
我有一个1列的矩阵:
> Control_Title_name
vehicle_scan_id4
153 "CL2004060801AA"
155 "CL2004060801AA"
232 "EC2004102602AA"
我想添加一个新列(名为"Class"),如下所示:
I want to add a new column (named "Class") like below:
> Control_Title_name
vehicle_scan_id4 Class
153 "CL2004060801AA" "Control"
155 "CL2004060801AA" "Control"
232 "EC2004102602AA" "Control"
第一列("vehicle_scan_id4")的长度是可变的,因此我希望控件"作为第二列(类")中的值出现在每一行中.
The length of first column ("vehicle_scan_id4") is variable so I want the "Control" appear in each row as a value in second column ("Class").
> Phenodata
Name FileName Target
153 "EC2004060203AA.CEL" "EC2004060203AA.CEL" "Treatment"
155 "EC2004060205AA.CEL" "EC2004060205AA.CEL" "Treatment"
232 "EC2004102606AA.CEL" "EC2004102606AA.CEL" "Treatment"
153 "CL2004060801AA.CEL" "CL2004060801AA.CEL" "Control"
155 "CL2004060801AA.CEL" "CL2004060801AA.CEL" "Control"
232 "EC2004102602AA.CEL" "EC2004102602AA.CEL" "Control"
这就是我最终矩阵的样子.有没有一种方法,我可以根据唯一行而不根据row.names
的值来进行过滤.例如,第4行和第5行包含完全相同的值.我的新矩阵是否可能仅包含其中之一,而不同时包含两者.
This is how my final matrix is looks like. Is there a way that I can filter only the unique rows based on their values not on row.names
. For example, 4th and 5th rows contains exactly the same values. Is it possible that my new matrix only contain one of them, not both.
推荐答案
您可以使用 cbind()
为此:
You can use cbind()
for this:
Control_Title_name <- matrix(c('CL2004060801AA','CL2004060801AA','EC2004102602AA'),3,dimnames=list(c('153','155','232'),c('vehicle_scan_id4')));
Control_Title_name;
## vehicle_scan_id4
## 153 "CL2004060801AA"
## 155 "CL2004060801AA"
## 232 "EC2004102602AA"
Control_Title_name <- cbind(Control_Title_name,Class='Control');
Control_Title_name;
## vehicle_scan_id4 Class
## 153 "CL2004060801AA" "Control"
## 155 "CL2004060801AA" "Control"
## 232 "EC2004102602AA" "Control"
回答第二个问题:
Control_Title_name[,'vehicle_scan_id4'] <- paste0(Control_Title_name[,'vehicle_scan_id4'],'.CEL');
Control_Title_name;
## vehicle_scan_id4 Class
## 153 "CL2004060801AA.CEL" "Control"
## 155 "CL2004060801AA.CEL" "Control"
## 232 "EC2004102602AA.CEL" "Control"
这篇关于在R的矩阵中添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!