[R] Use variable inside Function for updating the matrix

arun smartpink111 at yahoo.com
Tue May 29 17:55:23 CEST 2012


Hi Antony,

Try this,

> Obj_Name<-da$AGE
> Obj_Name
[1] 20 30 40 30 25

 da[Obj_Name>=30 & Obj_Name<=40, "TRUE/FALSE"]<-TRUE
colnames(da)<-c("NAME","Obj_Name","PLACE","ELIGIBLE")
> da
  NAME Obj_Name     PLACE ELIGIBLE
1  ABC       20     INDIA       NA
2  XYZ       30    FRANCE     TRUE
3  PQR       40       USA     TRUE
4  MNO       30     KENYA     TRUE


> da[is.na(da)]<-""> da
  NAME Obj_Name     PLACE ELIGIBLE
1  ABC       20     INDIA         
2  XYZ       30    FRANCE     TRUE
3  PQR       40       USA     TRUE
4  MNO       30     KENYA     TRUE
5  DEF       25 AUSTRALIA         





I hope this helps.
A.K.




----- Original Message -----
From: Rantony <antony.akkara at ge.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, May 29, 2012 3:17 AM
Subject: [R] Use variable inside Function for updating the matrix

Hi,

Here i have a data frame like this,
frame name is "dat"
*NAME          AGE       ELIGIBLE*
A                     20          
B                     14
C                     35
D                     10 
E                      50

In this, i want to change the column '*ELIGIBLE*' status to '*YES*' If, AGE
between 18 to 35
Which means that, i should get a put like this...
*NAME          AGE       ELIGIBLE*
A                     20          YES
B                     14
C                     35           YES
D                     10 
E                      50
----------------------------------------------
Now i got the solution for this, 
dat[dat$AGE<=35 & da$AGE>18,"ELIGIBLE"]<-YES

My issue is, i assigned AGE to a variable and that variable i tried inside
the function.
for eg:- 
*Obj_Name* <- AGE
dat[dat$*Obj_Name*<=35 & da$*Obj_Name*>18,"ELIGIBLE"]<-YES
Now, what is i getting the error !. 

So,could you please help me, what is the solution *pass column name through
*variable to this function ?

Can i get immediate solution please ? 

- Thanks
Antony

--
View this message in context: http://r.789695.n4.nabble.com/Use-variable-inside-Function-for-updating-the-matrix-tp4631663.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