user11418708
user11418708

Reputation: 902

Summing up data frames columns and rows

I have the below data.frame and I would like to identify the position of the highest value as well the index. For example, let's consider the below data.farme.

 index t1 t2 t3 t4
   10  1  4  7 10
   20  2  5  8 11
   30  3  6  9  0
   40  0  0  0  0 

In the first step, I would like to add up the rows of the data.frame.

index    t1 t2 t3 t4
  100    6  12 24 21

In the second step I would like to select the position (t) of the highest value. In this case, this would be t3 with the highest value 24.

In the third step, I would like to add columns t1-t4 and to identify the index with the highest value. In this case, this would be index 20 (highest value 26).

index t
10    22
20    26
30    18
40    0

Sample data:

df<-structure(list(index=c (10,20,30,40), 
                   t1 = c(1, 2, 3, 0), 
                   t2 = c(4, 5, 6, 0), 
                   t3 = c(7, 8,9,  0),
                   t4 = c(10, 11, 0, 0)), row.names = c(NA,4L), class = "data.frame")
                                                            
df

Upvotes: 0

Views: 46

Answers (3)

Ivn Ant
Ivn Ant

Reputation: 135

Using dplyr

Column sum :

df %>%
 summarise_all(sum)

select the highest value after adding up:

df %>%
 summarise_all(sum) %>%
 select(-index) %>%
 max()

adding up t1-t4 and select the row with the highest value:

df %>%
  mutate(t = rowSums(.[2:5])) %>%
  select(index,t) %>%
  filter(t == max(t)) 

Upvotes: 1

peter
peter

Reputation: 786

Columnwise:

df <- setDT(df)
df_c <- df[,t:=t1+t2+t3+t4][, .(index, t)]
df_c[, .(index = index[which.max(t)], t = max(t))]

Rowwise:

df_r <- df[, .(index = sum(index),
               t1 = sum(t1),
               t2 = sum(t2),
               t3 = sum(t3),
               t4 = sum(t4))]

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389235

In base R, you can do this with the help of rowSums and colSums.

#Column-wise sum
df1 <- colSums(df)
df1
#index    t1    t2    t3    t4 
#  100     6    15    24    21 

#Column name of highest value
highest_col <- names(df)[-1][which.max(df1[-1])]
highest_col
#[1] "t3"

#row-wise sum
df2 <- rowSums(df[-1])
df2
# 1  2  3  4 
#22 26 18  0 
#Corresponding index of highest row sum
highest_row_index <- df$index[which.max(df2)]
highest_row_index
#[1] 20

Upvotes: 1

Related Questions