How to assign values to a column for a subset of data frame rows

前端 未结 1 846
北海茫月
北海茫月 2021-01-16 23:41

I have a large data frame and I am trying to assign values to a particular data column for specific subsets.

subset(P2Y12R_binding_summary,(SYSTEM==\"4NTJ\")         


        
相关标签:
1条回答
  • 2021-01-17 00:35

    If speed is a concern I would look to data.table (I normally look there anyway).

    library(data.table)
    setDT(P2Y12R_binding_summary)[SYSTEM=="4NTJ" & VARIANT=="D294N",  Ki_expt := 42.2 ]
    
    an Example using diamonds:
    
        library(data.table)
        dummydf  <- diamonds
        setDT(dummydf)[cut =="Premium" & color =="J",  example := 42.2 ]
    
    dummydf[!is.na(example)]
         carat     cut color clarity depth table price    x    y    z example
      1:  0.30 Premium     J     SI2  59.3    61   405 4.43 4.38 2.61    42.2
      2:  1.00 Premium     J     SI2  62.3    58  2801 6.45 6.34 3.98    42.2
      3:  0.93 Premium     J     SI2  61.9    57  2807 6.21 6.19 3.84    42.2
      4:  1.17 Premium     J      I1  60.2    61  2825 6.90 6.83 4.13    42.2
      5:  0.33 Premium     J     VS1  62.8    58   557 4.41 4.38 2.76    42.2
     ---                                                                     
    804:  1.01 Premium     J      I1  60.7    59  2602 6.42 6.39 3.89    42.2
    805:  1.01 Premium     J     SI2  58.3    62  2683 6.49 6.43 3.77    42.2
    806:  1.01 Premium     J     SI2  59.3    56  2683 6.51 6.45 3.84    42.2
    807:  0.90 Premium     J     SI2  62.7    57  2717 6.09 6.06 3.80    42.2
    808:  0.90 Premium     J     SI2  63.0    59  2717 6.14 6.11 3.86    42.2
    

    Note that you only setDT() once. after that just call your DT using dummydf[subsets, LHS name := RHS value]

    0 讨论(0)
提交回复
热议问题