Home > OS >  How to sum multiple values in a entry in a dataframe in R?
How to sum multiple values in a entry in a dataframe in R?

Time:11-27

Lets say I have the following df:

teamid      scores
1            10
2            11

Where the first number (i.e. 10) is the first score and the second number (i.e. 10) is the second score. How would I sum the scores together in the df?

result df:

teamid      Combinedscore
1             1
2             2

CodePudding user response:

Here's a tidyr and dplyr solution:

library(tidyr)
library(dplyr)   
df %>%
  extract(scores, 
          into = c("1","2"), 
          regex = "(.)(.)",
          convert = TRUE) %>%
  mutate(combinedScore = rowSums(across(c("1","2")))) %>%
  select(-c(2,3))
  teamid combinedScore
1      1             1
2      2             2

Data:

df <- data.frame(
  teamid = c(1:2),
  scores = c(10, 11)
)

CodePudding user response:

We can use read.fwf from base R to split the scores column into multiple columns, and get the rowSums of each digit to create the 'Combinedscore'

df$Combinedscore <- rowSums(read.fwf(textConnection(as.character(df$scores)), 
       widths = rep(1, max(nchar(df$scores)))), na.rm = TRUE)

-output

> df
  teamid scores Combinedscore
1      1     10             1
2      2     11             2

data

df <- structure(list(teamid = 1:2, scores = 10:11), 
   class = "data.frame", row.names = c(NA, 
-2L))

CodePudding user response:

Here's a base R using strsplit with sapply

df$Combinedscore <- 
   colSums( sapply( strsplit( as.character(df$scores),"" ), as.numeric ) )

  teamid scores Combinedscore
1      1     10             1
2      2     11             2
  • Related