Hi all,
I'm hoping someone might help with a query about conditionally applying 
formulas to a dataframe.

In essence I have 3 lookup tables (Table A, B & C) and a dataframe with a 
variable Type.Code, which identifies the Lookup Table to which each record 
belongs. The lookup tables reference different sensor types for which I need 
apply a different formula to values in Column3 in each row to calculate a value 
for Column 4. I have no problem writing a for loop to handle this, but with 
over 1M rows in the df this is very slow.


Is there a way to solve this using a function such as sapply or would I be 
better off just subsetting and applying each function separately on the 
appropriate subset before rejoining.  

Thanks in advance.

Nick


Table.A
1
3
5
13

Table.B
4
6
10
20

Table.C

5321
3233
4532

Dataframe
Record    Type.code     Column3 Column4
1                       1               0.4                     
2                       3               0.25                    
3                       4               100                     
4                       20              150                     
5                       5               0.4                     
6                       4532    NA              


I have no problem writing a foor loop to do this
        for (i in 1:nrow(dataframe) ) {
        
                        if (Type.code[i]%in%Table.A) Reading[i]<-function 1     
        
                        else
                        if (Type.code[i]%in%tTable.B) Reading[i]<-function 2
                        else                                            
                        if (Type.code[i]%in%Table.C) Reading[i]<-function 3     
        
                        }


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dr Nick Bond
Research Fellow
Monash University
Victoria, Australia, 38000
Email: nick.b...@sci.monash.edu.au
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to