Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:22:44+00:00 2026-06-18T02:22:44+00:00

block.random() from the psych library is a good tool for creating block-randomized experimental designs,

  • 0

block.random() from the psych library is a good tool for creating block-randomized experimental designs, however, the function as-written requires that you do some calculations before you can start, and generates a matrix that only includes numerical indices for the levels of your experimental factors.

An example plant growth experiment, with the kind of information you typically know when setting out to do an experimental design:

  • two experimental factors, say fertilizer and sunlight
  • three levels of fertilizer (5mL/day, 10mL/day, 20mL/day)
  • two levels of sunlight (direct sun, shade)
  • your power analysis tells you you need 15 samples per group

You’d use block.random in the following way:

> plan=block.random(n=90,c(fertilizer=3,sunlight=2))
> headtail(plan)
    blocks fertilizer sunlight
S1       1          3        2
S2       1          2        1
S3       1          3        1
S4       1          1        1
...    ...        ...      ...
S87     15          3        2
S88     15          2        1
S89     15          1        1
S90     15          1        2

OK great, but two problems:

  • You’ve got to realize that you need 90 total samples before you can use the function (15 per group, times 3 levels of fertilizer, times 2 levels of sunlight) this is a trivial problem to solve
  • You can’t necessarily use the output directly, as you’ve got to correlate the numeric levels of the factors (1:3 for fertilizer, 1:2 for sunlight) to your actual levels (5mL/day, 10mL/day, 20mL/day for fertilizer) and (direct sun, shade for sunlight)

I’d rather have something including my level names that I can just directly print for reference or quickly format as a table for inclusion into a report.

How can I accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:22:45+00:00Added an answer on June 18, 2026 at 2:22 am

    With a simple wrapper function for block.random(), all of the above is possible.

    create.randomization.plan <- function(n.per.group, factors, seed=NULL){
      factor.lengths <- sapply(factors,length)
      if(!is.null(seed)){
        set.seed(seed)
      }
      plan <- as.data.frame(block.random(n.per.group*prod(factor.lengths),
                                         factor.lengths))
      for(i in 1:length(factors)){
        plan[,colnames(plan)==names(factors)[i]] <-
          factor(plan[,colnames(plan)==names(factors)[i]])
        levels(plan[,colnames(plan)==names(factors)[i]]) <- factors[[i]]
      }
      return(plan)
    }
    

    usage is like so:

    > factors <- list(fertilizer=c("5mL/day", "10mL/day", "20mL/day"),
    +                 sunlight=c("direct sun", "shade"))
    > 
    > plan <- create.randomization.plan(15, factors)
    > headtail(plan)
        blocks fertilizer   sunlight
    S1       1   10mL/day      shade
    S2       1   20mL/day direct sun
    S3       1   20mL/day      shade
    S4       1    5mL/day direct sun
    ...    ...       <NA>       <NA>
    S87     15   10mL/day      shade
    S88     15    5mL/day direct sun
    S89     15   10mL/day direct sun
    S90     15   20mL/day direct sun
    

    you can also set a seed for reproducibility: create.randomization.plan(15, factors, seed=123)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to create a block that shows a random image from a pool
Help me translate following block of the Haskell code. The run function produces text
I need to display 4 random subcategories from a parent category. I have the
I have a method for my app to read a random line from a
I'm trying to pull out certain groups of keywords from a block of text
this function gets a question from a database and is supposed to return it.
I ask this because I am creating a spider to collect data from blogger.com
Someone is creating erroneous links by adding a question mark and random characters to
i have to select a random row from a table and display it on
I am trying to read from /dev/random and /dev/urandom and would like to know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.