Sum only numerical columns and divide values by total

前端 未结 2 1353
不思量自难忘°
不思量自难忘° 2021-01-24 18:27

I am having trouble with some calculations on a data frame.

Here is my DF (with many more rows and columns)

What I am trying to do is:

Step (1) - For e

2条回答
  •  情书的邮戳
    2021-01-24 19:12

    Probably a lot less concise then Ronak's answer, but demonstrates the sweep functionality.

    Construct example df:

    df <- data.frame(cbind(Firm = c("A", "B", "C", "D"),
                           Assets_Jan_2018 = as.numeric(c(210234, 123144, 897897, 235467)),
                           Assets_Feb_2018 = as.numeric(c(235425, 127124, 789798, 897342)),
                           Returns_Jan_2018 = as.double(c(4.5,  5.3,  1.4, 9.7)),
                           Returns_Feb_2019 = as.double(c(6.7, 1.2, 6.2, 3.2))))
    

    Ensure numeric data types:

    df <- type.convert(df)
    

    Calculate the weighted returns:

    FirmWeightedReturns <- cbind(Firm = df$Firm,
                                 sweep(df[sapply(df, is.numeric) & !(grepl("returns", tolower(colnames(df))))],
                                       2,
                                       as.numeric(sapply(df[sapply(df, is.numeric) & !(grepl("returns", tolower(colnames(df))))], sum)), '/')
                                      * df[grepl("returns", tolower(colnames(df)))])
    

    Summarise as a df:

    TotalReturns <- data.frame(lapply(FirmWeightedReturns[sapply(FirmWeightedReturns, is.numeric)], sum))
    

    Output to console:

    TotalReturns
    

提交回复
热议问题