-3

I have a data frame that looks something like this.

     NAME   NUMBER
1    A      3
2    B      4
3    A      7
4    B      1

And I want it to look like

     NAME   NUMBER
1    A      10
2    B      5

What's the easiest way to do this for a data frame with 4932 rows?

jhaywoo8
  • 767
  • 5
  • 13
  • 23

1 Answers1

-2

You can do that with dplyr using group_by and summarize:

df1  <- read.table(text="NAME   NUMBER
A      3
B      4
A      7
B      1", header=TRUE, stringsAsFactors=FALSE)

library(dplyr)
df1 %>%
group_by(NAME) %>%
summarize(NUMBER=sum(NUMBER),NUMBER=sum(NUMBER))

# A tibble: 2 x 2
   NAME NUMBER
  <chr>  <int>
1     A     10
2     B      5
Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56