dimanche 9 mai 2021

conditional row indexing in r

I have a large dataset and I want to create a new column that sets a value based on a condition. Here is an example:

x <- tibble(
  x1 = c(rep("a", 3), rep("a1", 3), rep("a2", 3))
)

I would like a new column that that identifies all of the same values of column one. The end result should look like the following:

x <- tibble(
  x1 = c(rep("a", 3), rep("a1", 3), rep("a2", 3)),
  x2 = c(rep(1, 3), rep(2, 3), rep(3, 3))
)

Is there an easy way to do this? Maybe in dplyr? Thanks for the help.

Aucun commentaire:

Enregistrer un commentaire