Using lapply to create new columns based on old columns

后端 未结 3 1894
攒了一身酷
攒了一身酷 2021-01-28 19:14

My data looks as follows:

DF <- structure(list(No_Adjusted_Gross_Income = c(183454, 241199, 249506
), NoR_from_1_to_5000 = c(1035373, 4272260, 1124098), NoR_f         


        
3条回答
  •  长情又很酷
    2021-01-28 19:54

    This should work.. lapply() is not needed

    library( data.table )
    setDT( DF )
    DF[, (var) := as.data.table ( t( t( DF[, 2:3] ) * val ) ) ][]
    
    
    #    No_Adjusted_Gross_Income NoR_from_1_to_5000 NoR_from_5000_to_10000 AGI_from_1_to_5000 AGI_from_5000_to_10000
    # 1:                   183454            1035373                 319540         2588950187             2396550000
    # 2:                   241199            4272260                4826042        10682786130            36195315000
    # 3:                   249506            1124098                1959866         2810807049            14698995000
    

提交回复
热议问题