1

in R, I have a data set TABLE A and I would like to make it look like TABLE B. How can I manipulate it so that I can do so? Newbie here... Sorry if it's so blatantly simple

TABLE A 

 Team       Points
Raptors      101
Lakers       99
Raptors      104
Raptors      88
Celtics      89
Lakers       100
Celtics      112

TABLE B

 Team    Total_Points
Raptors      293
Lakers       199
Celtics      201
NickA
  • 433
  • 2
  • 10

1 Answers1

2

We can use aggregate

aggregate(cbind(Total_points = Points) ~ Team, df1, sum, 
        na.rm  = TRUE, na.action = NULL)
#     Team Total_points
#1 Celtics          201
#2  Lakers          199
#3 Raptors          293

Or with dplyr

library(dplyr)
df1 %>%
    group_by(Team) %>%
    summarise(Total_Points = sum(Points, na.rm = TRUE))

-output

# A tibble: 3 x 2
#  Team    Total_Points
#  <chr>          <int>
#1 Celtics          201
#2 Lakers           199
#3 Raptors          293

data

df1 <- structure(list(Team = c("Raptors", "Lakers", "Raptors", "Raptors", 
"Celtics", "Lakers", "Celtics"), Points = c(101L, 99L, 104L, 
88L, 89L, 100L, 112L)), class = "data.frame", row.names = c(NA, 
-7L))
akrun
  • 874,273
  • 37
  • 540
  • 662