[R] Fw: Column create and Update using function
arun
smartpink111 at yahoo.com
Fri Jul 13 16:07:36 CEST 2012
----- Forwarded Message -----
From: arun <smartpink111 at yahoo.com>
To: "Akkara, Antony (GE Energy, Non-GE)" <Antony.Akkara at ge.com>
Cc: R help <r-help at r-project.org>
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)" <Antony.Akkara at ge.com>
To: arun <smartpink111 at yahoo.com>
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:smartpink111 at yahoo.com]
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 <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