[R] Create and Change Column in a Matrix as per condition

arun smartpink111 at yahoo.com
Tue May 29 19:26:19 CEST 2012


Hi Antony,

Try this,
I changed matrix to data frame to make it work and change it back again to matrix.  There might be a easy way to do this.


 GetCsv<-as.matrix(data.frame("DWATT"=c(10,20,30,30,32)))
 GetCsv<-data.frame(GetCsv)
 GetCsv[GetCsv$DWATT<30 | GetCsv$DWATT >31, "DWAT_QF"] <-"RC"
 GetCsv[is.na(GetCsv)]<- ""
 GetCsv
  DWATT DWAT_QF
1    10      RC
2    20      RC
3    30        
4    30        
5    32      RC
 GetCsv<-as.matrix(GetCsv)
 GetCsv
     DWATT DWAT_QF
[1,] "10"  "RC"   
[2,] "20"  "RC"   
[3,] "30"  ""     
[4,] "30"  ""     
[5,] "32"  "RC"   


A.K.



----- Original Message -----
From: Rantony <antony.akkara at ge.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, May 29, 2012 9:13 AM
Subject: [R] Create and Change Column in a Matrix as per condition

Hi,

i have a matrix like this
  *DWATT *
    10              
    20            
    30              
    30              
    32              
Here i need to create a new column named "*DWATT_QF*" with filling value as
per the following condition,
if DWATT_QF = RC When DWATT value less than 30 and greater than 31. Which
means, i need output like this,

  *DWATT               DWATT_QF*  
    10                          RC
    20                          RC
    30                          RC
    30                          RC
    32                          RC

Here " GetCsv " is my matrix name. i tried this way and its getting proper
in "R",
GetCsv[(GetCsv[,ColHdr]< 30) | (GetCsv[,ColHdr]>31),ColHdr_QF]<-"RC"

But i *not getting in Eclips*, and getting error like "*Sub script Out of
Range*"

Could you please help me fast. Its an urgent !  


- Thanks 
Antony

--
View this message in context: http://r.789695.n4.nabble.com/Create-and-Change-Column-in-a-Matrix-as-per-condition-tp4631691.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list