Data containment <- R

Asked

Viewed 923 times

1

in the R (rstudio)

I have a vector v1 <- c(543, 543, 543, 675, 675, 675, 675, 22, 22, 22, 90, 90, 87, 876, 867).

I’d like to get a vector vr which represented the count of v1 (increasingly 1:n) of the repeating numbers.

In relation to the previous vector (v1) for example: vr <- c(1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 1, 2, 1, 1, 2).

From now on, thank you very much!

  • Try the function ?table. But the countdown doesn’t give the question.

  • I just found out:

  • 2

    I have just figured out how to do the task: https://answall.com/questions/357/howto count-o-n%C3%Bamero-cumulative-to-occur%C3%Aancias-to-one-element-to-one-vector/358#358 com: "count <- ave(rep(1, length(x))), x, FUN=cumsum)"

  • Thank you very much!

1 answer

1


For registration only, it is quite easy to do this with the dplyr package (se that a data frame, since it does not work with vector).

library(dplyr)

v1 <- data.frame(num=c(543, 543, 543, 675, 675, 675, 675, 22, 22, 22, 90, 90, 87, 876, 867))
vr <- v1 %>%
  group_by(num) %>%
  summarise(repetido = n()) %>%
  arrange(repetido)

Browser other questions tagged

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