[R] Column create and Update using function

arun smartpink111 at yahoo.com
Sat Jul 14 02:10:24 CEST 2012


Hi Antony,

There is still some confusion as to what you actually want as result.  

For example, your statement

->"In this i need to check each particular column values are between Max and
Min value.
If the coulmn value not coming between Max and Min, then i need to create
another coulmn"

This implies to check for each columns in the dataset and create colname_QF

My reply was based on these facts.  As the Min and Max were assigned to 3 and 6, I was under the assumption that this is for the whole dataset.  Then, you mentioned that it was only for column ABC.  I guess the Min and Max were the ones you found from the original dataset for the first column.


So, based on Min and Max for each column, your condition should be met. 


->"If the coulmn value not coming between Max and Min, then i need to create
another coulmn
by adding column name header with "_QF". and assign a string like "RC" for
that particular row."



If that is the case, try the code below:
####################
dat1<-read.table(text="
ABC   XYZ  PQR  RDN   SQT 
2     4    3     2     8
5     4    8     3     9
7     1    3     4     1
3     2    4     3     2
4     6    5     7     4 
",sep="",header=TRUE)

mindat1<-apply(dat1,2,min)
maxdat1<-apply(dat1,2,max)
minmaxdat<-data.frame(rbind(mindat1,maxdat1))
rownames(minmaxdat)<-1:nrow(minmaxdat)
 func1<-function(x,y,z)
 {ifelse(y[[x]] < max(z[[x]]) & y[[x]] > min(z[[x]]),y[[x]]<-"","RC")}
 dat2<-data.frame(sapply(names(dat1),function(x) func1(x,dat1,minmaxdat)))
colnames(dat2)<-paste(colnames(dat2),"QF",sep="_")
dat3<-data.frame(cbind(dat1,dat2))
dat3
  ABC XYZ PQR RDN SQT ABC_QF XYZ_QF PQR_QF RDN_QF SQT_QF
1   2   4   3   2   8     RC            RC     RC       
2   5   4   8   3   9                   RC            RC
3   7   1   3   4   1     RC     RC     RC            RC
4   3   2   4   3   2                                   
5   4   6   5   7   4            RC            RC       
##################################################

A.K.





----- Original Message -----
From: Rantony <antony.akkara at ge.com>
To: r-help at r-project.org
Cc: 
Sent: Friday, July 13, 2012 2:42 AM
Subject: [R] Column create and Update using function

Hi,

here i have a Max and Min values
Min <-3
Max <-6
and also a matrix like this,

ABC        XYZ         PQR
------       -------        -------
2                 4                3
5                 4                8
7                 1                3

In this i need to check each particular column values are between Max and
Min value.
If the coulmn value not coming between Max and Min, then i need to create
another coulmn
by adding column name header with "_QF". and assign a string like "RC" for
that particular row.

For eg:- i need to checkout coulmn "ABC".
Here  2,5,6 are the values we need to checkout with Min,Max values. and here
Min <-3,Max <-6
First need to create a new column called "ABC_QF" with current matrix.

ABC        XYZ         PQR          ABC_QF
------       -------        -------         -----------
2                 4                3               RC
5                 4                8
7                 1                3

Next, for 5 , it coming in between 3 to 6. so nothing to do.

ABC        XYZ         PQR          ABC_QF
------       -------        -------         -----------
2                 4                3               RC
5                 4                8
7                 1                3

Next, for 7 , its not coming in between 3 to 6. so put "RC"

ABC        XYZ         PQR          ABC_QF
------       -------        -------         -----------
2                 4                3               RC
5                 4                8
7                 1                3               RC
-----------------------------------------------------------
This is the requirement. i did it using for-loop,it will check each value
and it taking time when bulk of data come.
Any hope to do using lappy,appy kind of functions ? Because at a time
complete coulmn should get update.
Could you please help me urgently ?

- Thanks
Antony. 

--
View this message in context: http://r.789695.n4.nabble.com/Column-create-and-Update-using-function-tp4636400.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