0

I am using R and I have two vectors with the same number of elements the first variable has numerical data and the second variable has categorical data (yes or no) I would like to make 2 new numerical variables that comprise of the data from the first with the condition being that all the variables that coincide with a yes go in one new variable and the ones coinciding with a no go in the other new variable.
V1 = c(2.6, 4.6, 3.0, 5.7, 6.2, 8.8)
V2 <- c("yes", "yes", "no", "no", "yes", "no")

  • 1
    Please add a sample of your data, and an example of what you want the new vector to look like. Suggestions about how to ask a question are [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – Bryan Hanson Apr 05 '15 at 13:26
  • He had one, for some reason it got deleted by the OP themselves. – codingEnthusiast Apr 05 '15 at 13:36
  • 1
    yeah sorry I wasnt sure how to format the data to look like anything other than a blob of numbers that would be confusing and I cant put pictures yet – Nigel McDonald Apr 06 '15 at 02:23

1 Answers1

1

Try this:

V1 = c(2.6, 4.6, 3.0, 5.7, 6.2, 8.8)
V2 <- c("yes", "yes", "no", "no", "yes", "no")
clean <- tapply(V1, V2, list)
clean$no
[1] 3.0 5.7 8.8
clean$yes
[1] 2.6 4.6 6.2
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
  • This seems to be what I had in mind but will the original vector still be there in its original for, as I will still need it as it was for other things? – Nigel McDonald Apr 06 '15 at 02:22
  • I just tried it and now I understand its creating a new vector or something called clean, just the term scared me for a second and yes it worked perfectly, thank you :) – Nigel McDonald Apr 06 '15 at 02:31
  • 1
    Saying "thank you" is generally being avoided in Stack Overflow, if you think this question fully covers you, you can "accept" it (click that green tick next to the answer). Have a nice day. – codingEnthusiast Apr 07 '15 at 21:36