Home > database >  How i can calculate a function across all columns with one column but all the other column that have
How i can calculate a function across all columns with one column but all the other column that have

Time:09-14

i have a data frame that looks like this :

A B C D
1 0 2.1 6.2
2 3.2 3.2 0
3 4.4 NA 8.3
4 NA 0 0
5 NA NA 8.8
6 NA NA 0
7 30 0 9.1
8 30 6.6 0

I want to calculate the pariwise sum of A with all the columns (including itself) but only those who match the A but are NOT NA and NOT 0.

The ideal output must be :

A B C D
72 12.6 22.9 68.4
library(tidyverse)
A = seq(1:8)
B = c(0,3.2,4.4,NA,NA,NA,NA,NA)
C = c(2.1,3.2,NA,0,NA,NA,0,6.6)
D = c(6.2,0,8.3,0,8.8,0,9.1,0)
table = tibble(A,B,C,D);table

My effort is :

table%>%
  dplyr::filter(!is.na(A))%>%
  dplyr::summarise(across(everything(), ~ sum(.x,A),na.rm=TRUE))

How i can do this in R using dplyr ? Any help ?

CodePudding user response:

  1. base
sapply(table, \(x) sum((x   table$A)[!is.na(x) & x != 0]))
  1. dplyr
table %>%
  summarise(across(everything(), ~ sum((.x   A)[!is.na(.x) & .x != 0])))

# # A tibble: 1 × 4
#       A     B     C     D
#   <int> <dbl> <dbl> <dbl>
# 1    72  12.6  22.9  48.4
  • Related