0

Good morning. I would like to subset a data frame with the amount of elements which meet a certain condition. For instance:

COLOR   X    Y
RED     0    1
RED     0    1
WHITE   3    1

I'd like to have another dataframe with the amount of elements who have got the same X value and the same Y value, but I don't know how to build it.

COLOR   X    Y   AMOUNT
RED     0    1   2
WHITE   3    1   1

Thank you so much!

  • I just edited it. I'd like to have the amount of elements which have got the same X value and the same Y value, and have that amount as an attribute in the dataframe. –  Dec 07 '19 at 11:15
  • 1
    So you just want to count number of rows with same `COLOR` ,`X` and `Y` value? So in `dplyr`, you can do `df %>% count(COLOR, X, Y)`. Is that what you want ? – Ronak Shah Dec 07 '19 at 11:16

2 Answers2

0
data.frame(df[!duplicated(df),],AMOUNT=as.vector(table(df$COLOR)))

gives,

  COLOR X Y AMOUNT
1   RED 0 1      2
3 WHITE 3 1      1

Data:

df  <- read.table(text="COLOR   X    Y
RED     0    1
RED     0    1
WHITE   3    1",header=T,stringsAsFactors=FALSE)
maydin
  • 3,715
  • 3
  • 10
  • 27
0
df %>% 
  group_by(X, Y) %>% 
  mutate(AMOUNT = n()) %>% 
  distinct(X, Y, .keep_all = T)

# A tibble: 2 x 4
# Groups:   X, Y [2]
  COLOR     X     Y AMOUNT
  <chr> <int> <int>  <int>
1 RED       0     1      2
2 WHITE     3     1      1
Lennyy
  • 5,932
  • 2
  • 10
  • 23