6

I have a matrix with 1 column:

> Control_Title_name

vehicle_scan_id4
153 "CL2004060801AA"
155 "CL2004060801AA"
232 "EC2004102602AA"

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"

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"

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
user3253470
  • 191
  • 1
  • 4
  • 11
  • `Control_Title_name$Class = "Control"` Is this what you want to do? – Sidhha Aug 18 '15 at 13:57
  • At the moment my matrix contain only 1 column "vehicle_scan_id4" I want to add another column to the same matrix which contains a character ("Control") and it replicates throughout the length of column 1. – user3253470 Aug 18 '15 at 14:01

2 Answers2

12

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"

Answer to your second question:

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"
bgoldst
  • 34,190
  • 6
  • 38
  • 64
  • How I can append ".CEL" at the end of every value in 1st column (vehicle_scan_id4). So that values become CL2004060801AA.CEL ... EC2004102602AA.CEL ??? – user3253470 Aug 18 '15 at 14:36
  • can you please see the last answer to this question. I have asked about filtering, if you can help me in that context as well. – user3253470 Aug 18 '15 at 15:16
  • I found the solution for this as well: Unique_Phenodata <- Phenodata[!duplicated(Phenodata[,1]),] – user3253470 Aug 18 '15 at 15:29
  • Can you help me with this question [http://stackoverflow.com/questions/35484595/data-frame-merge-and-selection-of-values-which-are-common-in-2-data-frames] – user3253470 Feb 18 '16 at 16:22
0

alternative

library(dplyr) 

Control_Title_name %>% 
  as.data.frame() %>% 
  mutate(Class='Control')

   vehicle_scan_id4   Class
1   CL2004060801AA Control
2   CL2004060801AA Control
3   EC2004102602AA Control
giac
  • 4,261
  • 5
  • 30
  • 59
  • How I can append ".CEL" at the end of every value in 1st column (vehicle_scan_id4). So that values become CL2004060801AA.CEL ... EC2004102602AA.CEL ??? – user3253470 Aug 18 '15 at 14:34