Sum All Values in Every Column of a Data.Frame in R

Sum all values in every column of a data.frame in R

You can use function colSums() to calculate sum of all values. [,-1] ensures that first column with names of people is excluded.

 colSums(people[,-1])
Height Weight
199 425

Assuming there could be multiple columns that are not numeric, or that your column order is not fixed, a more general approach would be:

colSums(Filter(is.numeric, people))

How do I sum values across a data frame in R?

with apply:

df$count <- apply(df,1,function(x){sum(x>0)})

with rowSums:

df$count <- rowSums(df>0)

result:

       site1 site2 site3 site4 count
Salix 16.50 7.00 7.5 6.00 4
Betula 17.75 19.75 0.0 5.25 3
Alnus 0.00 0.00 0.0 0.00 0
Picea 0.00 0.35 0.0 0.00 1

data:

df <- read.table(text = "           site1     site2      site3      site4
Salix 16.50 7.00 7.50 6.00
Betula 17.75 19.75 0.00 5.25
Alnus 0.00 0.00 0.00 0.00
Picea 0.00 0.35 0.00 0.00")

How to sum data.frame column values?

You can just use sum(people$Weight).

sum sums up a vector, and people$Weight retrieves the weight column from your data frame.

Note - you can get built-in help by using ?sum, ?colSums, etc. (by the way, colSums will give you the sum for each column).

summing multiple columns in an R data-frame quickly

Here's an alternative approach using tidyverse:

library(tidyverse)

# input columns of interest
cols = c("mpg", "cyl", "disp", "hp", "drat")

mtcars %>%
group_by(id = row_number()) %>% # for each row
nest(cols) %>% # nest selected columns
mutate(SUM = map_dbl(data, sum)) # calculate the sum of those columns

# # A tibble: 32 x 3
# id data SUM
# <int> <list> <dbl>
# 1 1 <tibble [1 x 5]> 301.
# 2 2 <tibble [1 x 5]> 301.
# 3 3 <tibble [1 x 5]> 232.
# 4 4 <tibble [1 x 5]> 398.
# 5 5 <tibble [1 x 5]> 565.
# 6 6 <tibble [1 x 5]> 357.
# 7 7 <tibble [1 x 5]> 631.
# 8 8 <tibble [1 x 5]> 241.
# 9 9 <tibble [1 x 5]> 267.
# 10 10 <tibble [1 x 5]> 320.
# # ... with 22 more rows

The output here is a data frame containing the row id (id), the data used at each row (data) and the calculated sum (SUM).

You can get a vector of the calculated SUM if you add ... %>% pull(SUM).

Sum Values of Every Column in Data Frame with Conditional For Loop

Here's a dplyr solution. First, I define the data frame.

df <- read.table(text = "x    v1    v2    v3
1 0 1 5
2 4 2 10
3 5 3 15
4 1 4 20", header = TRUE)

# x v1 v2 v3
# 1 1 0 1 5
# 2 2 4 2 10
# 3 3 5 3 15
# 4 4 1 4 20

Then, I create a label (x_check) to indicate which group each row belongs to based on your criterion (x > 2), group by this label, and summarise each column with a v in its name using sum.

# Load library
library(dplyr)

df %>%
mutate(x_check = ifelse(x>2, "x1", "x2")) %>%
group_by(x_check) %>%
summarise_at(vars(contains("v")), funs(sum))

# # A tibble: 2 x 4
# x_check v1 v2 v3
# <chr> <int> <int> <int>
# 1 x1 6 7 35
# 2 x2 4 3 15

Sum up all previous values in column

As Andrew Gustar mentions, e.g.,

cars <- mtcars
cumcyl <- cumsum(cars$cyl)
cars <- cbind(cars, cumcyl)

Sum column values after every 10 rows in dataframe R

Here is a solution with data.table:

library("data.table")
D <- fread(
"11 2990000 3000000 0.00000000
11 3000000 3010000 2.30247191
11 3010000 3020000 0.32213483
11 3020000 3030000 0.91696629
11 3030000 3040000 1.81595506
11 3040000 3050000 0.27269663
11 3050000 3060000 2.21988764
11 3060000 3070000 3.44640449
11 3070000 3080000 2.02134831
11 3080000 3090000 1.22123596
11 3090000 3100000 3.47089888
11 3100000 3110000 3.08921348
11 3110000 3120000 3.11786517
11 3120000 3130000 1.44325843
11 3130000 3140000 0.00000000
11 3140000 3150000 0.00000000
11 3150000 3160000 2.55146067
11 3160000 3170000 0.63460674
11 3170000 3180000 1.08415730
11 3180000 3190000 2.73101124")

D[, .(V2=V2[1], V3=V3[.N], V4=sum(V4)), by=gl(D[, .N]/10, 10)]
# > D[, .(V2=V2[1], V3=V3[.N], V4=sum(V4)), by=gl(D[, .N]/10, 10)]
# gl V2 V3 V4
# 1: 1 2990000 3090000 14.53910
# 2: 2 3090000 3190000 18.12247


Related Topics



Leave a reply



Submit