Rowwise matrix multiplication in R

后端 未结 4 525
别跟我提以往
别跟我提以往 2021-01-18 23:05

I have a matrix with the dimension of 100 million records and 100 columns.

Now I want to multiply that matrix by rowwise.

My sample code for matrix multiplic

4条回答
  •  心在旅途
    2021-01-18 23:30

    Try package data.table with Reduce. That might avoid internal copies of a 1e10 length vector.

    library(data.table)
    df <- data.table(df, keep.rownames=TRUE)
    df[, rowprods:= Reduce("*", .SD), .SDcols = -1]
    df[, .(rn, rowprods)]
    #                     rn   rowprods
    # 1:           Mazda RX4          0
    # 2:       Mazda RX4 Wag          0
    # 3:          Datsun 710  609055152
    # 4:      Hornet 4 Drive          0
    # 5:   Hornet Sportabout          0
    # 6:             Valiant          0
    # 7:          Duster 360          0
    # 8:           Merc 240D          0
    # 9:            Merc 230          0
    #10:            Merc 280          0
    #11:           Merc 280C          0
    #12:          Merc 450SE          0
    #13:          Merc 450SL          0
    #14:         Merc 450SLC          0
    #15:  Cadillac Fleetwood          0
    #16: Lincoln Continental          0
    #17:   Chrysler Imperial          0
    #18:            Fiat 128  470578906
    #19:         Honda Civic  564655046
    #20:      Toyota Corolla  386281789
    #21:       Toyota Corona          0
    #22:    Dodge Challenger          0
    #23:         AMC Javelin          0
    #24:          Camaro Z28          0
    #25:    Pontiac Firebird          0
    #26:           Fiat X1-9  339825992
    #27:       Porsche 914-2          0
    #28:        Lotus Europa 1259677924
    #29:      Ford Pantera L          0
    #30:        Ferrari Dino          0
    #31:       Maserati Bora          0
    #32:          Volvo 142E 1919442833
    #                     rn    rowsums
    

    However, 8 GB RAM (minus what your OS and other software needs) is not much if you want to work with data of this size. R sometimes needs to make internal copies to use your data.

提交回复
热议问题