How can I add the identical values in the column of a data frame?

Asked

Viewed 159 times

1

The column has values such as: 1, 2, 3, NA. I need to group those who are equal. For example, if you have 4(1)s, the new table will have a row with value 4 representing the quantity of 1s.

inserir a descrição da imagem aqui

  • Hello, Bruno. Do you want to consider adding the rows, the columns or the whole date.frame? In the title you ask for an action and in the description you ask for another. What do you need to do? Edit your question so we can help.

  • Try tbl <- table(x);tbl <- data.frame(value = names(tbl), count = unclass(tbl)).

1 answer

5

You can do something like this:

df <- data.frame(x = c(1,1,2,2,2,3,3,3,4,4, NA))

df
    x
1   1
2   1
3   2
4   2
5   2
6   3
7   3
8   3
9   4
10  4
11 NA


library(dplyr)

df %>% 
   group_by(x) %>% 
   summarise(sum(x))
# A tibble: 5 x 2
      x `sum(x)`
  <dbl>    <dbl>
1     1        2
2     2        6
3     3        9
4     4        8
5    NA       NA

Browser other questions tagged

You are not signed in. Login or sign up in order to post.