Column create and Update using function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Column create and Update using function

R_Antony
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.
Reply | Threaded
Open this post in threaded view
|

Re: Column create and Update using function

arun kirshna
Hi,

Try this:

dat1<-read.table(text="
ABC        XYZ        PQR
2                4                3
5                4                8
7                1                3
",sep="",header=TRUE)

 newdat<-apply(dat1,2,function(x) ifelse(x<6 & x>3,"","RC"))
 colnames(newdat)<-paste(colnames(newdat),"QF",sep="_")
 dat2<-data.frame(cbind(dat1,newdat))
  ABC XYZ PQR ABC_QF XYZ_QF PQR_QF
1   2   4   3     RC            RC
2   5   4   8                   RC
3   7   1   3     RC     RC     RC
dat2
 
A.K.



----- Original Message -----
From: Rantony <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Fw: Column create and Update using function

arun kirshna




----- Forwarded Message -----
From: arun <[hidden email]>
To: "Akkara, Antony (GE Energy, Non-GE)" <[hidden email]>
Cc: R help <[hidden email]>
Sent: Friday, July 13, 2012 10:05 AM
Subject: Re: [R] Column create and Update using function

Hi,

I thought you want to check all the columns at once using "apply" or similar functions.

dat1[!(dat1$ABC<6 & dat1$ABC>3),"ABC_QF"]<-"RC"
dat1[is.na(dat1)]<-""
> dat1
  ABC XYZ PQR ABC_QF
1   2   4   3     RC
2   5   4   8      
3   7   1   3     RC
A.K.



----- Original Message -----
From: "Akkara, Antony (GE Energy, Non-GE)" <[hidden email]>
To: arun <[hidden email]>
Cc:
Sent: Friday, July 13, 2012 9:46 AM
Subject: RE: [R] Column create and Update using function

Hi Arun,

Here I need to check only with one particular column, not with all columns.
I tried with

newdat<-apply(dat1[,1],2,function(x) ifelse(x<6 & x>3,"","RC"))

then I getting an error like this:
" Error in apply(dat1[, 1], 1, function(x) ifelse(x < 6 & x > 3, "", "RC")) :
  dim(X) must have a positive length "



- thanks
Antony.

-----Original Message-----
From: arun [mailto:[hidden email]]
Sent: Friday, July 13, 2012 6:00 PM
To: Akkara, Antony (GE Energy, Non-GE)
Cc: R help
Subject: Re: [R] Column create and Update using function

Hi,

Try this:

dat1<-read.table(text="
ABC        XYZ        PQR
2                4                3
5                4                8
7                1                3
",sep="",header=TRUE)

 newdat<-apply(dat1,2,function(x) ifelse(x<6 & x>3,"","RC"))
 colnames(newdat)<-paste(colnames(newdat),"QF",sep="_")
 dat2<-data.frame(cbind(dat1,newdat))
  ABC XYZ PQR ABC_QF XYZ_QF PQR_QF
1   2   4   3     RC            RC
2   5   4   8                   RC
3   7   1   3     RC     RC     RC
dat2
 
A.K.



----- Original Message -----
From: Rantony <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Column create and Update using function

arun kirshna
In reply to this post by R_Antony
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 <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

RE: Column create and Update using function

R_Antony

Hi Arun,

I will explain more clearly what I need.

 

Here I have an array MinMax with 2 column Max and Min

 

MinMax – it is like

 

Min  Max

----  ------

3        6

1       5

 

 

And I have an matrix with same dimension [same dimension is must]

 

MyMatrix –

 

ABC    XYZ

----      -----

4              6

7              3

 

[Condition : if the matrix values is not coming in between Min and Max value then give “RC”]

 

Here , in the next step , 1st row of MinMax values [Min=3,Max=6] will compare with 1st Column of MyMatrix [“ABC”] and

as per that result will store as “RC” in a new column [actual coulmnname with “_QF”]. Like this,

 

 

[Here in ABC Column, we are going to compare the values 4 & 7 with MinMax values 3 & 6 and result will put in ABC_QF column]

[Same like above for next XYZ column]. Finally result will get like this form.

 

ABC    XYZ      ABC_QF    XYZ_QF

----      -----    ------             -------

4              6                              RC

7              3       RC                 

 

Its urgent.

 

-Thanks

Antony.

 

From: arun kirshna [via R] [mailto:[hidden email]]
Sent: Sunday, July 15, 2012 5:30 PM
To: Akkara, Antony (GE Energy, Non-GE)
Subject: Re: Column create and Update using function

 

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 <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.


If you reply to this email, your message will be added to the discussion below:

http://r.789695.n4.nabble.com/Column-create-and-Update-using-function-tp4636400p4636552.html

To unsubscribe from Column create and Update using function, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Column create and Update using function

arun kirshna


Hi Antony,

It's the same code, but made a little modification.  I think still you can use my previous code as a more generic one as it will calculate the 'min' and 'max' for each columns if that is that case.

#Anyway, try this:
##################################################
minmax<-read.table(text="
Min  Max
3     6

1     5
",sep="",header=TRUE)

mydat1<-read.table(text="
ABC    XYZ
4       6
7       3
",sep="",header=TRUE)

minmax1<-t(minmax)
colnames(minmax1)<-colnames(mydat1)
minmax2<-data.frame(minmax1)
func1<-function(x,y,z)
{ifelse(y[[x]] < max(z[[x]]) & y[[x]] > min(z[[x]]),y[[x]]<-"",y[[x]]<-"RC")}
mydat2<-data.frame(sapply(names(mydat1),function(x) func1(x,mydat1,minmax2)))
colnames(mydat2)<-paste(colnames(mydat1),"QF",sep="_")
mydat3<-data.frame(cbind(mydat1,mydat2))

mydat3
  ABC XYZ ABC_QF XYZ_QF
1   4   6            RC
2   7   3     RC      

########################################
A.K.




----- Original Message -----
From: Rantony <[hidden email]>
To: [hidden email]
Cc:
Sent: Monday, July 16, 2012 8:17 AM
Subject: Re: [R] Column create and Update using function

Hi Arun,

I will explain more clearly what I need.



Here I have an array MinMax with 2 column Max and Min



MinMax – it is like



Min  Max

----  ------

3        6

1       5





And I have an matrix with same dimension [same dimension is must]



MyMatrix –



ABC    XYZ

----      -----

4              6

7              3



[Condition : if the matrix values is not coming in between Min and Max value then give “RC”]



Here , in the next step , 1st row of MinMax values [Min=3,Max=6] will compare with 1st Column of MyMatrix [“ABC”] and

as per that result will store as “RC” in a new column [actual coulmnname with “_QF”]. Like this,





[Here in ABC Column, we are going to compare the values 4 & 7 with MinMax values 3 & 6 and result will put in ABC_QF column]

[Same like above for next XYZ column]. Finally result will get like this form.



ABC    XYZ      ABC_QF    XYZ_QF

----      -----    ------             -------

4              6                              RC

7              3       RC                 



Its urgent.



-Thanks

Antony.



From: arun kirshna [via R] [mailto:[hidden email]]
Sent: Sunday, July 15, 2012 5:30 PM
To: Akkara, Antony (GE Energy, Non-GE)
Subject: Re: Column create and Update using function



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 <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.



________________________________

If you reply to this email, your message will be added to the discussion below:

http://r.789695.n4.nabble.com/Column-create-and-Update-using-function-tp4636400p4636552.html 

To unsubscribe from Column create and Update using function, click here < .
NAML <
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml



--
View this message in context: http://r.789695.n4.nabble.com/Column-create-and-Update-using-function-tp4636400p4636639.html
Sent from the R help mailing list archive at Nabble.com.
    [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

RE: Column create and Update using function

R_Antony

Hi Arun. Thank a lot. This code working fine ! – Thanks a lot.

 

From: arun kirshna [via R] [mailto:[hidden email]]
Sent: Tuesday, July 17, 2012 8:05 AM
To: Akkara, Antony (GE Energy, Non-GE)
Subject: Re: Column create and Update using function

 



Hi Antony,

It's the same code, but made a little modification.  I think still you can use my previous code as a more generic one as it will calculate the 'min' and 'max' for each columns if that is that case.

#Anyway, try this:
##################################################
minmax<-read.table(text="
Min  Max
3     6

1     5
",sep="",header=TRUE)

mydat1<-read.table(text="
ABC    XYZ
4       6
7       3
",sep="",header=TRUE)

minmax1<-t(minmax)
colnames(minmax1)<-colnames(mydat1)
minmax2<-data.frame(minmax1)
func1<-function(x,y,z)
{ifelse(y[[x]] < max(z[[x]]) & y[[x]] > min(z[[x]]),y[[x]]<-"",y[[x]]<-"RC")}
mydat2<-data.frame(sapply(names(mydat1),function(x) func1(x,mydat1,minmax2)))
colnames(mydat2)<-paste(colnames(mydat1),"QF",sep="_")
mydat3<-data.frame(cbind(mydat1,mydat2))

mydat3
  ABC XYZ ABC_QF XYZ_QF
1   4   6            RC
2   7   3     RC      

########################################
A.K.




----- Original Message -----
From: Rantony <[hidden email]>
To: [hidden email]
Cc:
Sent: Monday, July 16, 2012 8:17 AM
Subject: Re: [R] Column create and Update using function

Hi Arun,

I will explain more clearly what I need.



Here I have an array MinMax with 2 column Max and Min



MinMax – it is like



Min  Max

----  ------

3        6

1       5





And I have an matrix with same dimension [same dimension is must]



MyMatrix –



ABC    XYZ

----      -----

4              6

7              3



[Condition : if the matrix values is not coming in between Min and Max value then give “RC”]



Here , in the next step , 1st row of MinMax values [Min=3,Max=6] will compare with 1st Column of MyMatrix [“ABC”] and

as per that result will store as “RC” in a new column [actual coulmnname with “_QF”]. Like this,





[Here in ABC Column, we are going to compare the values 4 & 7 with MinMax values 3 & 6 and result will put in ABC_QF column]

[Same like above for next XYZ column]. Finally result will get like this form.



ABC    XYZ      ABC_QF    XYZ_QF

----      -----    ------             -------

4              6                              RC

7              3       RC                 



Its urgent.



-Thanks

Antony.



From: arun kirshna [via R] [mailto:[hidden email]]
Sent: Sunday, July 15, 2012 5:30 PM
To: Akkara, Antony (GE Energy, Non-GE)
Subject: Re: Column create and Update using function



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 <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.



________________________________

If you reply to this email, your message will be added to the discussion below:

http://r.789695.n4.nabble.com/Column-create-and-Update-using-function-tp4636400p4636552.html 

To unsubscribe from Column create and Update using function, click here < .
NAML <
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml



--
View this message in context: http://r.789695.n4.nabble.com/Column-create-and-Update-using-function-tp4636400p4636639.html
Sent from the R help mailing list archive at Nabble.com.
    [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.


If you reply to this email, your message will be added to the discussion below:

http://r.789695.n4.nabble.com/Column-create-and-Update-using-function-tp4636400p4636716.html

To unsubscribe from Column create and Update using function, click here.
NAML