0

I have two columns in data frame. I want to count the total number of each V (e.g. IGHV4-39) in R. Here I am adding an example of my dataset.

      v count

IGHV4-39 1541

IGHV3-66 1094

IGHV3-7 1038

IGHV4-39 794

IGHV3-23 748

IGHV3-66 727

IGHV3-7 608

I want to get the result as following-

IGHV4-39 2335

IGHV3-66 1821

IGHV3-7 1646

IGHV3-23 748

[eExample: For IGHV4-39> 1541+794 =2335]

How can I count such way in R? It would be great if someone writes the solution.

Thanks and Cheers Sumanta

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Sumanta B
  • 11
  • 1

1 Answers1

0
df <- read.table(text =
    "v count
     IGHV4-39 1541
     IGHV3-66 1094
     IGHV3-7 1038
     IGHV4-39 794
     IGHV3-23 748
     IGHV3-66 727
     IGHV3-7 608",
  header = TRUE)

# Base R
aggregate(count ~ v, FUN = sum, data = df)

# With dplyr
library(dplyr)
df %>% 
  group_by(v) %>% 
  summarize(count = sum(count))

# With data.table
library(data.table)
df2 <- as.data.table(df)
df2[, .(count = sum(count)), by = v]
Michael M
  • 880
  • 7
  • 10