Create a random list generator in R that runs multiple times

We are often showing participants different stimuli that are randomly ordered to prevent ordering effects. To randomize the stimuli we often used random.org’s list randomizer, but a current limitation of their site is you can only randomize a list once. So, for instance, if we were testing 4 stimuli with four people, we would need to use random.org to randomize that list four different times.

Here is some simple code to create a random list generator that will resample a list as many times as you want. You enter the number of items in your list, the number of times you want to draw down your entire list, and define the items in your list. The code below puts the list items into a single column and then prints the results to a CSV file. The example code has 4 items in the list and will randomize those 4 items 2 separate times. Sampling is done without replacement.

# Enter the number of items in your list
x <- 4

#Enter the number of times you want the list to run
r <- 2

#Add the items that are in your list. Add to or remove the number of items if necessary
mylist <- c('Item 1','Item 2','Item 3','Item 4')

#Below is the randomizer
randommatrix <- matrix(nrow=x,ncol=r)
for (i in 1:r)
{randommatrix[,i]=sample(mylist,x,replace=FALSE)}
y <- x*r
randommatrixlist <- matrix(randommatrix,nrow=y,ncol=1)
print(randommatrixlist)

#Optional: Print your randomization list to a CSV file. Delete the "#" to activate
write.csv(randommatrixlist,'randomlist.csv')

Continue reading “Create a random list generator in R that runs multiple times”

Create a formatted contingency table for Word using R

One of the bigger challenges I have had with R is getting output into a shareable format. There are helpful packages, such as gmodels, that can produce nice-looking contingency tables in the console, but getting the results out of the console and into Word is difficult. For example, gmodels can produce a table like this…

   Cell Contents
|-------------------------|
|                       N |
| Chi-square contribution |
|           N / Row Total |
|           N / Col Total |
|         N / Table Total |
|-------------------------|

 
Total Observations in Table:  248 

 
                 | infert$induced 
infert$education |         0 |         1 |         2 | Row Total | 
-----------------|-----------|-----------|-----------|-----------|
          0-5yrs |         4 |         2 |         6 |        12 | 
                 |     1.232 |     0.506 |     9.898 |           | 
                 |     0.333 |     0.167 |     0.500 |     0.048 | 
                 |     0.028 |     0.029 |     0.162 |           | 
                 |     0.016 |     0.008 |     0.024 |           | 
-----------------|-----------|-----------|-----------|-----------|
         6-11yrs |        78 |        27 |        15 |       120 | 
                 |     1.121 |     1.059 |     0.471 |           | 
                 |     0.650 |     0.225 |     0.125 |     0.484 | 
                 |     0.545 |     0.397 |     0.405 |           | 
                 |     0.315 |     0.109 |     0.060 |           | 
-----------------|-----------|-----------|-----------|-----------|
         12+ yrs |        61 |        39 |        16 |       116 | 
                 |     0.518 |     1.627 |     0.099 |           | 
                 |     0.526 |     0.336 |     0.138 |     0.468 | 
                 |     0.427 |     0.574 |     0.432 |           | 
                 |     0.246 |     0.157 |     0.065 |           | 
-----------------|-----------|-----------|-----------|-----------|
    Column Total |       143 |        68 |        37 |       248 | 
                 |     0.577 |     0.274 |     0.149 |           | 
-----------------|-----------|-----------|-----------|-----------|

…which is nice, but doesn’t work well for Word.

I found this wonderful solution as suggested by David Gohel in this forum comment post. (David is also the author of the ReporteRs package).

Continue reading “Create a formatted contingency table for Word using R”

Getting RStudio Markdown to work the first time

As I mentioned earlier, I’m the process of learning R and RStudio. As I hit stumbling blocks with RStudio, I’m posting solutions here. I’m posting partially as a reference for me so I can remember how to fix a problem, but also in hopes it helps others.

Problem: Running a command in the console works perfectly, but when creating an R Markdown file in R Studio, it fails.

Imagine that I have a data frame called “farmers” that contains data on the amount of produce a farmer sells at farmer’s market. The data frame came from a .csv file that I imported into RStudio. 

There is a column of data called “Quantity” that contains the quantity of produce that is sold. In RStudio, I run the following command in the console:

summary(farmers$Quantity)

And the following result is returned in the console:

Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
182.0   452.5   782.0  1696.0   969.5  9183.0 

I put the exact same command in the R Markdown file as follows:

```{r}
summary(farmers$Quantity)
```

And this error message is returned:

Error in summary(farmers$Quantity) : object ‘farmers’ not found Calls: … withCallingHandler -> withVisible -> eval -> eval -> summary 

Execution halted

This is a terribly arcane message that, when translated to English, is saying that the data frame “farmers” could not be found. This is puzzling because in the console RStudio is able to find the data frame. If it works in the console, why not in R Markdown? 

The issue is involves the spirit of replicability that is so important to R. The R Markdown file requires to you to show all the steps you took to produce your data, and this includes showing the step where you loaded your data frame.

To fix this problem you first need to, if you have not already, save your data frame as an .Rdata file. With my data frame called “farmers”, that console command is:

save(farmers, file = "farmers.RData")

Second, in your R Markdown file, you need to tell RStudio load that RData file. For me, code is:

```{r}
load("farmers.RData")
```

Press “Knit HTML” and it should now work. RStudio can now find the data frame to produce results from. 

One thing to also note is that if you use a package, you also have to load that in your R Markdown file. Again, this goes back to the spirit of replicability and showing your work so that others could rerun the same analysis you did. An example code to load a package would be:

library(gmodels)

If the package “gmodels” was being used.

Fixing a y-axis that is too short in barplot function in R

Over the holidays I invested some time in learning R and RStudio, something I have wanted to do for some time now. The opportunity presented itself when needing to help a family member run chi-square tests, something more involved in Excel than I would have guessed (Excel requires you to manually calculate the expected values table). The experience learning R has been fun but there are occasional head-scratching frustrations. As I go through the learning process I’ll be posting here mainly as a reference for me so I can remember how I solved various problems, but hopefully it will provide help to others who have hit stumbling blocks.

One issues I had a problem with was with the barplot function. Take for example this code that contains 100 observations (we’ll say it’s the number of times a person visits a particular store in a month), produces a table from these data, and then creates a barplot from the table.

visits <- c(2, 2, 3, 7, 10, 3, 1, 0, 8, 7, 11, 14, 1, 3, 0, 8, 9, 4, 3, 3, 20, 9, 2, 5, 12, 3, 6, 1, 1, 2, 4, 2, 2, 3, 9, 13, 7 ,11, 15, 15, 19, 7, 8, 7, 6, 0, 6, 0, 1, 4, 2, 9, 0, 6, 12, 7, 6, 14, 5, 0, 4, 0, 0, 8, 0, 4, 4, 1, 3, 5, 6, 15, 1, 6, 13, 2, 1, 3, 5, 3, 19, 12, 3, 0, 7, 0, 2, 4, 2, 2, 2, 5, 1, 4, 3, 0, 6, 11, 0, 3)

visits.table <- table (visits)
barplot(visits.table)

This is the resulting chart.

Chart with bad x-axis

At first I thought I had a nice looking bar chart, only to notice that, while the data had a maximum frequency of 13, the y-axis cut short at 12. Also when the x-axis legend numbers become double digits they become too wide, leading R to begin skipping every other one. (A low tech way to fix this is widen the plot window.)

Continue reading “Fixing a y-axis that is too short in barplot function in R”