0

I am trying to generate a list in r with two unique components, "x' and "y". In this list I need n of "x" and p of "y".

For example; I want three "dog" and two "cat" so that my list looks like

list = c("dog", "dog", "dog", "cat", "cat")

How do I go about this?

  • I agree with moooh's answer. If you decide you need a list after all, you can do as.list(animals) after following his idea. In addition, it is wise to not use the pre-existing R function name "list" as a variable name. That might cause troubles later on. – MartijnVanAttekum May 29 '18 at 09:18

1 Answers1

0

In R lists and vectors are different things. I think you need a vector. To accoplish what you want you can use the rep() function like so:

n <- 5
p <- 2
animals <- c(rep("dog",n),rep("cat",p))
moooh
  • 459
  • 3
  • 10