Home > Back-end >  How to automatically fill in a blank column
How to automatically fill in a blank column

Time:09-22

I am trying to get the list of sums of two columns from my original data set, from left to right enter image description here

I have made a loop:

for (i in 1:ncol(df)) { 
m = i
n = i   1
if (i %% 2 != 0) {
df_cum$V1 <- sum(df[,m]   df[,n])
}
}

But, the way to add value to the new list is wrong:

df_cum$V1 <- sum(df[,m]   df[,n])

would be really appreciated if anyone knows how to do that in R

CodePudding user response:

You can try split.default(), i.e.

sapply(split.default(df, gsub('\\d ', '', names(df))), sum)
 A  B 
17 12 

CodePudding user response:

A base R option using tapply -

tapply(unlist(df), 
       rep(1:ncol(df), each = nrow(df) * 2, length.out = nrow(df) * ncol(df)), 
       sum)

# 1  2  3 
#17 12 13 

The logic here is to create group of every 2 columns and sum them.

data

It is easier to help if you provide data in a reproducible format

df <- data.frame(A1 = c(0, 3, 2), A2 = c(2, 6, 4), 
                 B1 = c(3, 0, 1), B2 = c(2, 3, 3), 
                 C1 = c(7, 3, 2), C2 = c(1, 0, 0))

CodePudding user response:

We can do this in tidyverse

library(dplyr)
library(tidyr)
df1 %>%   
    pivot_longer(everything(), names_to = c(".value", "grp"), 
           names_sep ="(?<=[A-Z])(?=[0-9])") %>%
    select(-grp) %>%    
    summarise(across(everything(), sum, na.rm = TRUE), .groups = 'drop')

-output

# A tibble: 1 x 3
      A     B     C
  <dbl> <dbl> <dbl>
1    17    12    13

Or using base R

aggregate(values ~ ., transform(stack(df1), 
            ind = sub("\\d ", "", ind)), FUN = sum)
  ind values
1   A     17
2   B     12
3   C     13

Or another option with rowsum from base R

with(stack(df1), rowsum(values, group = trimws(ind, whitespace = "\\d ")))
  [,1]
A   17
B   12
C   13

Or another option is with colSums and rowsum

{tmp <- colSums(df1); rowsum(tmp, group = substr(names(tmp), 1, 1))}
  [,1]
A   17
B   12
C   13

data

df1 <- structure(list(A1 = c(0, 3, 2), A2 = c(2, 6, 4), B1 = c(3, 0, 
1), B2 = c(2, 3, 3), C1 = c(7, 3, 2), C2 = c(1, 0, 0)), 
  class = "data.frame", row.names = c(NA, 
-3L))
  • Related