Joel B Joel B - 3 months ago 10
R Question

Add a total row summation columns into a dataframe in R

I have a dataframe made up to multiple volume columns only and I want to create a total column called test which sits in the dataframe. The below code will work if I just set

test <- ...
but if I add
summary_transposed_no_time$
to the left, the code doesn't seem to add it to the dataframe.

I would also like to know how I could evolve this piece of code so that I could create test to be all columns minus column 1, and then later in the process create another test column (maybe called test2) which would be a summation of all columns minus column 2 - I can hard code the column positions but not the column names (as they can change in naming convention each time the code is run) so I haven't included them here

w <- ncol(summary_transposed_no_time)
summary_transposed_no_time$test <- apply(summary_transposed_no_time[,c(1:w)], 1, sum)


Example of summary_transposed_no_time:

postal_dist_a | postal_dist_b | postal_dist_c
------------- | ------------- | -------------
20 | 25 | 15
25 | 40 | 23
31 | 32 | 19
24 | 39 | 17
37 | 19 | 26


Desired result columns within summary_transposed_no_time:

postal_dist_a | postal_dist_b | postal_dist_c | test
------------- | ------------- | ------------- | -------------
20 | 25 | 15 | 60
25 | 40 | 23 | 88
31 | 32 | 19 | 82
24 | 39 | 17 | 80
37 | 19 | 26 | 82

Answer

You should provide a reprocucible example. But if your question is really just about how to do rowsums, I would rather use the built in function rowSums. Your code would be :

set.seed(1)
# I recreate a table more or less like yours
summary_transposed_no_time=data.frame(matrix(rnorm(1000),ncol=5))
n=ncol(summary_transposed_no_time)

# Test that contains the rowsum
summary_transposed_no_time$test=rowSums(summary_transposed_no_time)

# test1 rowsum minus column 1
summary_transposed_no_time$testm1=rowSums(summary_transposed_no_time[,2:n])
# test2 rowsum minus column 2
summary_transposed_no_time$testm2=rowSums(summary_transposed_no_time[,c(1,3:n)])
#test_i minus column i
i=3
summary_transposed_no_time$testmi=rowSums(summary_transposed_no_time[,c(1:n)][,-i])

#check on first line :
sum(summary_transposed_no_time[1,1:n])==summary_transposed_no_time$test[1]
sum(summary_transposed_no_time[1,2:n])==summary_transposed_no_time$testm1[1]
sum(summary_transposed_no_time[1,c(1,3:n)])==summary_transposed_no_time$testm2[1]
sum(summary_transposed_no_time[1,c(1:2,4:n)])==summary_transposed_no_time$testmi[1]