Aggregate data in one column based on values in another column

前端 未结 4 1708
轮回少年
轮回少年 2021-02-14 00:10

I know there is an easy way to do this...but, I can\'t figure it out.

I have a dataframe in my R script that looks something like this:

A      B    C
1.2         


        
4条回答
  •  名媛妹妹
    2021-02-14 00:32

    In dplyr:

    library(tidyverse)
    A <- c(1.2, 2.3, 2.3, 1.2, 3.4, 1.2)
    B <- c(4, 4, 6, 3, 2, 5)
    C <- c(8, 9, 0, 3, 1, 1)
    
    df <- data_frame(A, B, C)
    
    df %>%
        group_by(A) %>% 
        summarise(num = n(),
                  totalB = sum(B))
    

提交回复
热议问题