Note that there are some explanatory texts on larger screens.

plurals
  1. POAlternative to for-loop to fill data.frame by unique rows
    primarykey
    data
    text
    <p>I am trying to make a script to generate a random set of people with demographic information using R. I want it to generate by rows and not columns so that a function can be based on the outcome of the previous function in the same row. I know this can be done with a for loop (as I did below) but for loops are extremely slow in R. I have read that you can use <em>apply</em> or <em>while</em> to do a loop much more efficiently but I haven't figured out how despite many failed attempts. A sample of the functional code with the loop is below. How would I do that with <em>apply</em> or <em>while</em>?</p> <pre><code>y &lt;- 1980 ## MedianYr d &lt;- 0.1 ## Rate of NA responses AgeFn &lt;- function(y){ Year &lt;- 1900 + as.POSIXlt(Sys.Date())$year RNormYr &lt;- as.integer((rnorm(1)*10+y)) Age &lt;- Year - RNormYr } EduByAge &lt;- function (Age, d) { ifelse(Age &lt; 17, sample(c("Some High School",NA), size=1,prob=c((1-d),d)), ifelse(Age &gt; 16 &amp; Age &lt; 19, sample(c("Some High School", "High School Grad",NA), size=1, prob=c(0.085, 0.604,d)), ifelse(Age &gt; 18 &amp; Age &lt; 21, sample(c("Some High School", "High School Grad", "Associates",NA), size=1,prob=c(0.085, 0.25, 0.354,d)), ifelse(20 &gt; Age &amp; Age &lt; 23, sample(c("Some High School", "High School Grad", "Associates", "Bachelors",NA), size=1,prob=c(0.085, 0.25, 0.075, 0.279,d)), ifelse(Age &gt; 22, sample(c("Some High School", "High School Grad", "Associates", "Bachelors", "Masters", "Professional", "Doctorate",NA),size=1,prob=c(0.085, 0.25, 0.075, 0.176, 0.072, 0.019, 0.012,d)), NA))))) } GenderFn &lt;- function(d){ Gender1 &lt;- sample(c("Male","Female","Trans", NA), 1, replace=TRUE, prob=c(0.49, 0.5, 0.01, d)) return(Gender1) } UserGen &lt;- function(n,s) { set.seed(s) Rows &lt;- function(y,d){ Age &lt;- abs(AgeFn(y)) Gender &lt;- GenderFn(d) Education &lt;- EduByAge(Age,d) c(i, Age, Gender, Education) } df &lt;- data.frame(matrix(NA, ncol = 4, nrow = n)) for(i in (1:n)) { df[i,] &lt;- Rows(y,d) } colnames(df) &lt;- c("ID", "Age", "Gender", "Education") return(df) } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload