2

I am trying to create a data frame with two columns (ID, Condition). Each ID is linked with 8 conditions. The ID's start at s009 and will range to s050. For each ID, I will have the same set of conditions. I have included a sample set of what I am to create for reference. I would greatly appreciate any help with this. Thanks in advance!

ID     Condition
s009    Baseline
s009    Meditation
s009    Practice
s009    Creativity
s009    Preblock 1
s009    Postblock 1
s009    Preblock 2
s009    Postblock 2
s010    Baseline
s010    Mediation
s010    Practice 
s010    Creativity
s010    Preblock 1
s010    Postblock 1
s010    Preblock 2
s010    Postblock 2
s011    Baseline
...
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
S. Basran
  • 21
  • 2
  • Possible duplicate of [tidyr - unique way to get combinations (using tidyverse only)](https://stackoverflow.com/questions/46491410/tidyr-unique-way-to-get-combinations-using-tidyverse-only) – NelsonGon May 02 '19 at 05:45

3 Answers3

2

One option would be expand.grid from base R (without using any external packages)

df1 <- expand.grid(ID = v1, Condition = v2)

Or using data.table

library(data.table)
CJ(ID = v1, Condition = v2)
#    ID   Condition
#  1: s009    Baseline
#  2: s009  Creativity
#  3: s009  Meditation
#  4: s009 Postblock 1
#  5: s009 Postblock 2
# ---                 
#332: s050 Postblock 1
#333: s050 Postblock 2
#334: s050    Practice
#335: s050  Preblock 1
#336: s050  Preblock 2

Or using tidyverse

library(tidyverse)
tibble(ID = v1) %>% 
    expand(ID, Condition = v2)
# A tibble: 336 x 2
#   ID    Condition  
#   <chr> <chr>      
# 1 s009  Baseline   
# 2 s009  Creativity 
# 3 s009  Meditation 
# 4 s009  Postblock 1
# 5 s009  Postblock 2
# 6 s009  Practice   
# 7 s009  Preblock 1 
# 8 s009  Preblock 2 
# 9 s010  Baseline   
#10 s010  Creativity 
# … with 326 more rows

where

v1 <- sprintf("s%03d", 9:50)
v2 <- c("Baseline", "Meditation", "Practice", "Creativity",
    "Preblock 1", "Postblock 1", "Preblock 2", "Postblock 2")
akrun
  • 874,273
  • 37
  • 540
  • 662
1

We can create two vectors, ID and Codition and use crossing

Condition <- c("Baseline","Meditation", "Practice", "Creativity" , "Preblock 1", 
             "Postblock 1", "Preblock 2", "Postblock 2")
ID <- paste0("s", sprintf("%03d", 9:50))

tidyr::crossing(ID, Condition)

#   ID    Condition  
#   <chr> <chr>      
# 1 s009  Baseline   
# 2 s009  Creativity 
# 3 s009  Meditation 
# 4 s009  Postblock 1
# 5 s009  Postblock 2
# 6 s009  Practice   
# 7 s009  Preblock 1 
# 8 s009  Preblock 2 
# 9 s010  Baseline   
#10 s010  Creativity 
# … with 326 more rows

We can use merge as well in base R

merge(ID, Condition)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks! If I wanted to export this into a .csv, how would I go about doing so? – S. Basran May 03 '19 at 06:41
  • @S.Basran store it in a variable, `out <- merge(ID, Condition)` and then use `write.csv(out, "path/to/store/file.csv", row.names = NULL)`. – Ronak Shah May 03 '19 at 06:42
0

Here is another solution:

library(stringr)

ID <- sort(rep(paste0('s', str_pad(9:50, width=3, side='left', pad='0')),8))
Condition <- rep(c('Baseline', 'Meditation', 'Practice', 'Creativity', 'Preblock 1', 'Postblock 1', 'Preblock 2', 'Postblock 2'), 8*42)

df <- data.frame(ID, Condition)
akorn
  • 70
  • 6