3

I am trying to write R code that

  1. creates a new variable
  2. populates that variable only if METRO=0
  3. the values that are populated are a random assignment of "1" and "2" values.

I tried something like this but it did not work:

iPUMS_2016 <- mutate (iPUMS_2016, metrounk = ifelse(METRO==0,mutate(rand_int=sample.int(n())) ))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 17 '20 at 20:27

1 Answers1

5

We don't need the second mutate inside the ifelse. Also, it could be a if/else condition i.e. if all the elements in 'METRO' are 0 then get the sample.int on the number of rows

library(dplyr)
mutate(iPUMS_2016, metrounk = if(all(METRO == 0)) sample.int(n()) else  METRO)

If it is to replace only elements that are 0.

mutate(iPUMS_2016, metrounk = ifelse(METRO == 0, sample.int(n()), METRO))

The sample.int(n()) is not clear. Or it should be sample.int(sum(METRO == 0))

mutate(iPUMS_2016, metrounk = replace(METRO, METRO == 0, 
               sample.int(sum(METRO == 0, na.rm = TRUE))))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you this is very helpful. Wondering what I would need to use with or instead of sample.int(n()) in order to get randomly assigned "1" and "2" values to the new variable "metrounk" – Quantitative72 Jan 18 '20 at 20:34
  • @Quantitative72 I guess you need `sample(c("1", "2", n(), replace = TRUE)` – akrun Jan 18 '20 at 21:15
  • I tried this 'iPUMS_2016 <- mutate(iPUMS_2016, metrounk = ifelse(METRO == 0, sample(c("5", "6", n(), replace = TRUE), METRO)))' but got this Error in ifelse(METRO == 0, sample(c("5", "6", n(), replace = TRUE), METRO)) : argument "no" is missing, with no default – Quantitative72 Feb 04 '20 at 00:31
  • @Quantitative72 I think it is a mistake on your part `sample(c("5", "6", n()` The `c()` bracket is not closed – akrun Feb 04 '20 at 00:33
  • Thanks for identifying the bracket issue. After running my freq look like that below. Any idea why it is giving me "3156487" as a metrounk value and not "6"? n 1 327920 2 307598 3 776585 3156487 169633 4 1235818 5 338933 – Quantitative72 Feb 04 '20 at 00:39
  • @Quantitative72 Can you use `dput` to sshow the example as I can't get the exact structure – akrun Feb 04 '20 at 00:43
  • Not sure how to use dput. Are you looking for the output table or the full dataset? – Quantitative72 Feb 04 '20 at 00:47
  • @Quantitative72 I just need 10 - 15 rows i.e. `dput(head(droplevels(iris)))` iris is inbuilt example, change that to your dataest. Just run it on your console and copy/paste the output – akrun Feb 04 '20 at 00:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207133/discussion-between-quantitative72-and-akrun). – Quantitative72 Feb 04 '20 at 00:53