简体   繁体   English

将总计行求和列添加到R中的数据框中

[英]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. 我有一个仅由多个卷列组成的数据框,并且我想创建一个称为test的总列,该列位于该数据框中。 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. 如果仅将test <- ...设置为test <- ... ,下面的代码将起作用test <- ...但是如果在左侧添加summary_transposed_no_time$ ,则该代码似乎未将其添加到数据框中。

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 我还想知道如何改进这段代码,以便我可以将所有列减去第1列创建测试,然后在该过程的稍后创建另一个测试列(可能称为test2),将所有列的总和减去第2列的列-我可以对列位置进行硬编码,但不能对列名称进行硬编码(因为它们可以在每次运行代码时更改其命名约定),因此我此处未包括它们

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: 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: 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

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. 但是,如果您的问题确实只是关于如何做rowums,我宁愿使用内置函数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]

I've found how to create a "total_" for every column within the df. 我发现了如何为df中的每一列创建一个“ total_”。 total_1 is the sum of all columns minus column 1, total_2 the sum of all columns minus column 2 etc etc total_1是所有列的总和减去列1,total_2是所有列的总和减去列2,依此类推

n=ncol(summary_transposed_no_time)

for (h in 1:ncol(summary_transposed_no_time)) {

  summary_transposed_no_time[,paste0("total_",h)] <- rowSums(summary_transposed_no_time[,c(1:n)][,-h])
  m = ncol(summary_transposed_no_time)
  print(paste("added in a total columns for region", h, "so the column count is now : ",m))

} # end for (h in 1:nrow(filtered_data_contents)){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM