Using tidyverse to randomly get means from conditions











up vote
0
down vote

favorite












I have a long format data frame that looks like this:
enter image description here



What I'd like to do is (1) randomly split the trials from Condition 1 into 4 groups and then calculate the mean of Y for each ID and (2) carry out this same procedure for the trials in Condition 2. This is what this new output would look like:



enter image description here



Is there a concise way to do this using the tidyverse? I'm just getting started and am having a having a hard time with this!










share|improve this question






















  • How big is your original data frame?
    – Harro Cyranka
    Nov 7 at 17:04















up vote
0
down vote

favorite












I have a long format data frame that looks like this:
enter image description here



What I'd like to do is (1) randomly split the trials from Condition 1 into 4 groups and then calculate the mean of Y for each ID and (2) carry out this same procedure for the trials in Condition 2. This is what this new output would look like:



enter image description here



Is there a concise way to do this using the tidyverse? I'm just getting started and am having a having a hard time with this!










share|improve this question






















  • How big is your original data frame?
    – Harro Cyranka
    Nov 7 at 17:04













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a long format data frame that looks like this:
enter image description here



What I'd like to do is (1) randomly split the trials from Condition 1 into 4 groups and then calculate the mean of Y for each ID and (2) carry out this same procedure for the trials in Condition 2. This is what this new output would look like:



enter image description here



Is there a concise way to do this using the tidyverse? I'm just getting started and am having a having a hard time with this!










share|improve this question













I have a long format data frame that looks like this:
enter image description here



What I'd like to do is (1) randomly split the trials from Condition 1 into 4 groups and then calculate the mean of Y for each ID and (2) carry out this same procedure for the trials in Condition 2. This is what this new output would look like:



enter image description here



Is there a concise way to do this using the tidyverse? I'm just getting started and am having a having a hard time with this!







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 16:26









user2917781

575




575












  • How big is your original data frame?
    – Harro Cyranka
    Nov 7 at 17:04


















  • How big is your original data frame?
    – Harro Cyranka
    Nov 7 at 17:04
















How big is your original data frame?
– Harro Cyranka
Nov 7 at 17:04




How big is your original data frame?
– Harro Cyranka
Nov 7 at 17:04












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Assuming that your data frame is not that small, this should do exactly what you need.



set.seed(2)
df <- tibble(ID = c(rep(1,50),rep(2,50)),
Condition = rep(c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2),5),
Y = rnorm(100,100,20)) ##Creates some fake data



result_df <- df %>% mutate(randomizer = sapply(1:length(row_number()), function(i)sample(c(1,2,3,4),1))) %>%
group_by(Condition) %>%
group_by(ID, Condition, randomizer) %>%
summarize(mean_y = mean(Y, na.rm = TRUE)) %>%
mutate(condition_status = paste0("Condition",Condition,"M",randomizer)) %>%
ungroup() %>%
dplyr::select(-Condition, -randomizer) %>%
spread(condition_status, mean_y)


result_df





share|improve this answer























  • are you only using the tidyverse package? I ask because I run this code and get the error "Error in select(., -Condition, -randomizer) : unused arguments (-Condition, -randomizer)". Wondering if this might go away if I loaded another package.
    – user2917781
    Nov 7 at 18:28










  • Yes, this was done using tidyverse. There is probably a conflict with the select function. Use dplyr::select(-Condition, -randomizer). Will update my answer
    – Harro Cyranka
    Nov 7 at 18:29












  • Yes, this works now!
    – user2917781
    Nov 7 at 18:34










  • Great. Would you mind approving my answer?
    – Harro Cyranka
    Nov 7 at 18:34


















up vote
1
down vote













Not the best way but it does work



x <- data.frame(c(rep(1,10), rep(2,10)), c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2), c(100,200,80,58,89,100,200,80,58,89,95,72,99,120,130,95,72,99,120,130))
colnames(x) <- c("ID", "Condition", "Y")
id <- unique(x[,1])
con <- unique(x[,2])
#multiple by the number of groups to split into
y <- data.frame(matrix(, ncol = (length(id) * 4) + 1, nrow = length(id)))
y[,1] <- id
colnames(y) <- c("ID", "Condition1M1", "Condition1M2", "Condition1M3", "Condition1M4", "Condition2M1", "Condition2M2", "Condition2M3", "Condition2M4")
x1 <- x[which(x[,2] == con[1]),]
x2 <- x[which(x[,2] == con[2]),]
#specify sample size
n <- 5
x11 <- x1[sample(nrow(x1), n, replace = FALSE),]
x12 <- x1[sample(nrow(x1), n, replace = FALSE),]
x13 <- x1[sample(nrow(x1), n, replace = FALSE),]
x14 <- x1[sample(nrow(x1), n, replace = FALSE),]
x21 <- x2[sample(nrow(x2), n, replace = FALSE),]
x22 <- x2[sample(nrow(x2), n, replace = FALSE),]
x23 <- x2[sample(nrow(x2), n, replace = FALSE),]
x24 <- x2[sample(nrow(x2), n, replace = FALSE),]

y[1,2] <- mean(x11[which(x11[,1] == id[1]),3])
y[1,3] <- mean(x12[which(x12[,1] == id[1]),3])
y[1,4] <- mean(x13[which(x13[,1] == id[1]),3])
y[1,5] <- mean(x14[which(x14[,1] == id[1]),3])
y[2,2] <- mean(x21[which(x21[,1] == id[2]),3])
y[2,3] <- mean(x22[which(x22[,1] == id[2]),3])
y[2,4] <- mean(x23[which(x23[,1] == id[2]),3])
y[2,5] <- mean(x24[which(x24[,1] == id[2]),3])
y[1,6] <- mean(x11[which(x11[,1] == id[1]),3])
y[1,7] <- mean(x12[which(x12[,1] == id[1]),3])
y[1,8] <- mean(x13[which(x13[,1] == id[1]),3])
y[1,9] <- mean(x14[which(x14[,1] == id[1]),3])
y[2,6] <- mean(x21[which(x21[,1] == id[2]),3])
y[2,7] <- mean(x22[which(x22[,1] == id[2]),3])
y[2,8] <- mean(x23[which(x23[,1] == id[2]),3])
y[2,9] <- mean(x24[which(x24[,1] == id[2]),3])


Results:



  ID Condition1M1 Condition1M2 Condition1M3 Condition1M4 Condition2M1 Condition2M2 Condition2M3 Condition2M4
1 1 133.3333 100 150 133.3333 133.3333 100 150 133.3333
2 2 125.0000 125 125 120.0000 125.0000 125 125 120.0000





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53193673%2fusing-tidyverse-to-randomly-get-means-from-conditions%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Assuming that your data frame is not that small, this should do exactly what you need.



    set.seed(2)
    df <- tibble(ID = c(rep(1,50),rep(2,50)),
    Condition = rep(c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2),5),
    Y = rnorm(100,100,20)) ##Creates some fake data



    result_df <- df %>% mutate(randomizer = sapply(1:length(row_number()), function(i)sample(c(1,2,3,4),1))) %>%
    group_by(Condition) %>%
    group_by(ID, Condition, randomizer) %>%
    summarize(mean_y = mean(Y, na.rm = TRUE)) %>%
    mutate(condition_status = paste0("Condition",Condition,"M",randomizer)) %>%
    ungroup() %>%
    dplyr::select(-Condition, -randomizer) %>%
    spread(condition_status, mean_y)


    result_df





    share|improve this answer























    • are you only using the tidyverse package? I ask because I run this code and get the error "Error in select(., -Condition, -randomizer) : unused arguments (-Condition, -randomizer)". Wondering if this might go away if I loaded another package.
      – user2917781
      Nov 7 at 18:28










    • Yes, this was done using tidyverse. There is probably a conflict with the select function. Use dplyr::select(-Condition, -randomizer). Will update my answer
      – Harro Cyranka
      Nov 7 at 18:29












    • Yes, this works now!
      – user2917781
      Nov 7 at 18:34










    • Great. Would you mind approving my answer?
      – Harro Cyranka
      Nov 7 at 18:34















    up vote
    2
    down vote



    accepted










    Assuming that your data frame is not that small, this should do exactly what you need.



    set.seed(2)
    df <- tibble(ID = c(rep(1,50),rep(2,50)),
    Condition = rep(c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2),5),
    Y = rnorm(100,100,20)) ##Creates some fake data



    result_df <- df %>% mutate(randomizer = sapply(1:length(row_number()), function(i)sample(c(1,2,3,4),1))) %>%
    group_by(Condition) %>%
    group_by(ID, Condition, randomizer) %>%
    summarize(mean_y = mean(Y, na.rm = TRUE)) %>%
    mutate(condition_status = paste0("Condition",Condition,"M",randomizer)) %>%
    ungroup() %>%
    dplyr::select(-Condition, -randomizer) %>%
    spread(condition_status, mean_y)


    result_df





    share|improve this answer























    • are you only using the tidyverse package? I ask because I run this code and get the error "Error in select(., -Condition, -randomizer) : unused arguments (-Condition, -randomizer)". Wondering if this might go away if I loaded another package.
      – user2917781
      Nov 7 at 18:28










    • Yes, this was done using tidyverse. There is probably a conflict with the select function. Use dplyr::select(-Condition, -randomizer). Will update my answer
      – Harro Cyranka
      Nov 7 at 18:29












    • Yes, this works now!
      – user2917781
      Nov 7 at 18:34










    • Great. Would you mind approving my answer?
      – Harro Cyranka
      Nov 7 at 18:34













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Assuming that your data frame is not that small, this should do exactly what you need.



    set.seed(2)
    df <- tibble(ID = c(rep(1,50),rep(2,50)),
    Condition = rep(c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2),5),
    Y = rnorm(100,100,20)) ##Creates some fake data



    result_df <- df %>% mutate(randomizer = sapply(1:length(row_number()), function(i)sample(c(1,2,3,4),1))) %>%
    group_by(Condition) %>%
    group_by(ID, Condition, randomizer) %>%
    summarize(mean_y = mean(Y, na.rm = TRUE)) %>%
    mutate(condition_status = paste0("Condition",Condition,"M",randomizer)) %>%
    ungroup() %>%
    dplyr::select(-Condition, -randomizer) %>%
    spread(condition_status, mean_y)


    result_df





    share|improve this answer














    Assuming that your data frame is not that small, this should do exactly what you need.



    set.seed(2)
    df <- tibble(ID = c(rep(1,50),rep(2,50)),
    Condition = rep(c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2),5),
    Y = rnorm(100,100,20)) ##Creates some fake data



    result_df <- df %>% mutate(randomizer = sapply(1:length(row_number()), function(i)sample(c(1,2,3,4),1))) %>%
    group_by(Condition) %>%
    group_by(ID, Condition, randomizer) %>%
    summarize(mean_y = mean(Y, na.rm = TRUE)) %>%
    mutate(condition_status = paste0("Condition",Condition,"M",randomizer)) %>%
    ungroup() %>%
    dplyr::select(-Condition, -randomizer) %>%
    spread(condition_status, mean_y)


    result_df






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 7 at 18:29

























    answered Nov 7 at 17:50









    Harro Cyranka

    978513




    978513












    • are you only using the tidyverse package? I ask because I run this code and get the error "Error in select(., -Condition, -randomizer) : unused arguments (-Condition, -randomizer)". Wondering if this might go away if I loaded another package.
      – user2917781
      Nov 7 at 18:28










    • Yes, this was done using tidyverse. There is probably a conflict with the select function. Use dplyr::select(-Condition, -randomizer). Will update my answer
      – Harro Cyranka
      Nov 7 at 18:29












    • Yes, this works now!
      – user2917781
      Nov 7 at 18:34










    • Great. Would you mind approving my answer?
      – Harro Cyranka
      Nov 7 at 18:34


















    • are you only using the tidyverse package? I ask because I run this code and get the error "Error in select(., -Condition, -randomizer) : unused arguments (-Condition, -randomizer)". Wondering if this might go away if I loaded another package.
      – user2917781
      Nov 7 at 18:28










    • Yes, this was done using tidyverse. There is probably a conflict with the select function. Use dplyr::select(-Condition, -randomizer). Will update my answer
      – Harro Cyranka
      Nov 7 at 18:29












    • Yes, this works now!
      – user2917781
      Nov 7 at 18:34










    • Great. Would you mind approving my answer?
      – Harro Cyranka
      Nov 7 at 18:34
















    are you only using the tidyverse package? I ask because I run this code and get the error "Error in select(., -Condition, -randomizer) : unused arguments (-Condition, -randomizer)". Wondering if this might go away if I loaded another package.
    – user2917781
    Nov 7 at 18:28




    are you only using the tidyverse package? I ask because I run this code and get the error "Error in select(., -Condition, -randomizer) : unused arguments (-Condition, -randomizer)". Wondering if this might go away if I loaded another package.
    – user2917781
    Nov 7 at 18:28












    Yes, this was done using tidyverse. There is probably a conflict with the select function. Use dplyr::select(-Condition, -randomizer). Will update my answer
    – Harro Cyranka
    Nov 7 at 18:29






    Yes, this was done using tidyverse. There is probably a conflict with the select function. Use dplyr::select(-Condition, -randomizer). Will update my answer
    – Harro Cyranka
    Nov 7 at 18:29














    Yes, this works now!
    – user2917781
    Nov 7 at 18:34




    Yes, this works now!
    – user2917781
    Nov 7 at 18:34












    Great. Would you mind approving my answer?
    – Harro Cyranka
    Nov 7 at 18:34




    Great. Would you mind approving my answer?
    – Harro Cyranka
    Nov 7 at 18:34












    up vote
    1
    down vote













    Not the best way but it does work



    x <- data.frame(c(rep(1,10), rep(2,10)), c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2), c(100,200,80,58,89,100,200,80,58,89,95,72,99,120,130,95,72,99,120,130))
    colnames(x) <- c("ID", "Condition", "Y")
    id <- unique(x[,1])
    con <- unique(x[,2])
    #multiple by the number of groups to split into
    y <- data.frame(matrix(, ncol = (length(id) * 4) + 1, nrow = length(id)))
    y[,1] <- id
    colnames(y) <- c("ID", "Condition1M1", "Condition1M2", "Condition1M3", "Condition1M4", "Condition2M1", "Condition2M2", "Condition2M3", "Condition2M4")
    x1 <- x[which(x[,2] == con[1]),]
    x2 <- x[which(x[,2] == con[2]),]
    #specify sample size
    n <- 5
    x11 <- x1[sample(nrow(x1), n, replace = FALSE),]
    x12 <- x1[sample(nrow(x1), n, replace = FALSE),]
    x13 <- x1[sample(nrow(x1), n, replace = FALSE),]
    x14 <- x1[sample(nrow(x1), n, replace = FALSE),]
    x21 <- x2[sample(nrow(x2), n, replace = FALSE),]
    x22 <- x2[sample(nrow(x2), n, replace = FALSE),]
    x23 <- x2[sample(nrow(x2), n, replace = FALSE),]
    x24 <- x2[sample(nrow(x2), n, replace = FALSE),]

    y[1,2] <- mean(x11[which(x11[,1] == id[1]),3])
    y[1,3] <- mean(x12[which(x12[,1] == id[1]),3])
    y[1,4] <- mean(x13[which(x13[,1] == id[1]),3])
    y[1,5] <- mean(x14[which(x14[,1] == id[1]),3])
    y[2,2] <- mean(x21[which(x21[,1] == id[2]),3])
    y[2,3] <- mean(x22[which(x22[,1] == id[2]),3])
    y[2,4] <- mean(x23[which(x23[,1] == id[2]),3])
    y[2,5] <- mean(x24[which(x24[,1] == id[2]),3])
    y[1,6] <- mean(x11[which(x11[,1] == id[1]),3])
    y[1,7] <- mean(x12[which(x12[,1] == id[1]),3])
    y[1,8] <- mean(x13[which(x13[,1] == id[1]),3])
    y[1,9] <- mean(x14[which(x14[,1] == id[1]),3])
    y[2,6] <- mean(x21[which(x21[,1] == id[2]),3])
    y[2,7] <- mean(x22[which(x22[,1] == id[2]),3])
    y[2,8] <- mean(x23[which(x23[,1] == id[2]),3])
    y[2,9] <- mean(x24[which(x24[,1] == id[2]),3])


    Results:



      ID Condition1M1 Condition1M2 Condition1M3 Condition1M4 Condition2M1 Condition2M2 Condition2M3 Condition2M4
    1 1 133.3333 100 150 133.3333 133.3333 100 150 133.3333
    2 2 125.0000 125 125 120.0000 125.0000 125 125 120.0000





    share|improve this answer

























      up vote
      1
      down vote













      Not the best way but it does work



      x <- data.frame(c(rep(1,10), rep(2,10)), c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2), c(100,200,80,58,89,100,200,80,58,89,95,72,99,120,130,95,72,99,120,130))
      colnames(x) <- c("ID", "Condition", "Y")
      id <- unique(x[,1])
      con <- unique(x[,2])
      #multiple by the number of groups to split into
      y <- data.frame(matrix(, ncol = (length(id) * 4) + 1, nrow = length(id)))
      y[,1] <- id
      colnames(y) <- c("ID", "Condition1M1", "Condition1M2", "Condition1M3", "Condition1M4", "Condition2M1", "Condition2M2", "Condition2M3", "Condition2M4")
      x1 <- x[which(x[,2] == con[1]),]
      x2 <- x[which(x[,2] == con[2]),]
      #specify sample size
      n <- 5
      x11 <- x1[sample(nrow(x1), n, replace = FALSE),]
      x12 <- x1[sample(nrow(x1), n, replace = FALSE),]
      x13 <- x1[sample(nrow(x1), n, replace = FALSE),]
      x14 <- x1[sample(nrow(x1), n, replace = FALSE),]
      x21 <- x2[sample(nrow(x2), n, replace = FALSE),]
      x22 <- x2[sample(nrow(x2), n, replace = FALSE),]
      x23 <- x2[sample(nrow(x2), n, replace = FALSE),]
      x24 <- x2[sample(nrow(x2), n, replace = FALSE),]

      y[1,2] <- mean(x11[which(x11[,1] == id[1]),3])
      y[1,3] <- mean(x12[which(x12[,1] == id[1]),3])
      y[1,4] <- mean(x13[which(x13[,1] == id[1]),3])
      y[1,5] <- mean(x14[which(x14[,1] == id[1]),3])
      y[2,2] <- mean(x21[which(x21[,1] == id[2]),3])
      y[2,3] <- mean(x22[which(x22[,1] == id[2]),3])
      y[2,4] <- mean(x23[which(x23[,1] == id[2]),3])
      y[2,5] <- mean(x24[which(x24[,1] == id[2]),3])
      y[1,6] <- mean(x11[which(x11[,1] == id[1]),3])
      y[1,7] <- mean(x12[which(x12[,1] == id[1]),3])
      y[1,8] <- mean(x13[which(x13[,1] == id[1]),3])
      y[1,9] <- mean(x14[which(x14[,1] == id[1]),3])
      y[2,6] <- mean(x21[which(x21[,1] == id[2]),3])
      y[2,7] <- mean(x22[which(x22[,1] == id[2]),3])
      y[2,8] <- mean(x23[which(x23[,1] == id[2]),3])
      y[2,9] <- mean(x24[which(x24[,1] == id[2]),3])


      Results:



        ID Condition1M1 Condition1M2 Condition1M3 Condition1M4 Condition2M1 Condition2M2 Condition2M3 Condition2M4
      1 1 133.3333 100 150 133.3333 133.3333 100 150 133.3333
      2 2 125.0000 125 125 120.0000 125.0000 125 125 120.0000





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Not the best way but it does work



        x <- data.frame(c(rep(1,10), rep(2,10)), c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2), c(100,200,80,58,89,100,200,80,58,89,95,72,99,120,130,95,72,99,120,130))
        colnames(x) <- c("ID", "Condition", "Y")
        id <- unique(x[,1])
        con <- unique(x[,2])
        #multiple by the number of groups to split into
        y <- data.frame(matrix(, ncol = (length(id) * 4) + 1, nrow = length(id)))
        y[,1] <- id
        colnames(y) <- c("ID", "Condition1M1", "Condition1M2", "Condition1M3", "Condition1M4", "Condition2M1", "Condition2M2", "Condition2M3", "Condition2M4")
        x1 <- x[which(x[,2] == con[1]),]
        x2 <- x[which(x[,2] == con[2]),]
        #specify sample size
        n <- 5
        x11 <- x1[sample(nrow(x1), n, replace = FALSE),]
        x12 <- x1[sample(nrow(x1), n, replace = FALSE),]
        x13 <- x1[sample(nrow(x1), n, replace = FALSE),]
        x14 <- x1[sample(nrow(x1), n, replace = FALSE),]
        x21 <- x2[sample(nrow(x2), n, replace = FALSE),]
        x22 <- x2[sample(nrow(x2), n, replace = FALSE),]
        x23 <- x2[sample(nrow(x2), n, replace = FALSE),]
        x24 <- x2[sample(nrow(x2), n, replace = FALSE),]

        y[1,2] <- mean(x11[which(x11[,1] == id[1]),3])
        y[1,3] <- mean(x12[which(x12[,1] == id[1]),3])
        y[1,4] <- mean(x13[which(x13[,1] == id[1]),3])
        y[1,5] <- mean(x14[which(x14[,1] == id[1]),3])
        y[2,2] <- mean(x21[which(x21[,1] == id[2]),3])
        y[2,3] <- mean(x22[which(x22[,1] == id[2]),3])
        y[2,4] <- mean(x23[which(x23[,1] == id[2]),3])
        y[2,5] <- mean(x24[which(x24[,1] == id[2]),3])
        y[1,6] <- mean(x11[which(x11[,1] == id[1]),3])
        y[1,7] <- mean(x12[which(x12[,1] == id[1]),3])
        y[1,8] <- mean(x13[which(x13[,1] == id[1]),3])
        y[1,9] <- mean(x14[which(x14[,1] == id[1]),3])
        y[2,6] <- mean(x21[which(x21[,1] == id[2]),3])
        y[2,7] <- mean(x22[which(x22[,1] == id[2]),3])
        y[2,8] <- mean(x23[which(x23[,1] == id[2]),3])
        y[2,9] <- mean(x24[which(x24[,1] == id[2]),3])


        Results:



          ID Condition1M1 Condition1M2 Condition1M3 Condition1M4 Condition2M1 Condition2M2 Condition2M3 Condition2M4
        1 1 133.3333 100 150 133.3333 133.3333 100 150 133.3333
        2 2 125.0000 125 125 120.0000 125.0000 125 125 120.0000





        share|improve this answer












        Not the best way but it does work



        x <- data.frame(c(rep(1,10), rep(2,10)), c(1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1,1,1,2,2), c(100,200,80,58,89,100,200,80,58,89,95,72,99,120,130,95,72,99,120,130))
        colnames(x) <- c("ID", "Condition", "Y")
        id <- unique(x[,1])
        con <- unique(x[,2])
        #multiple by the number of groups to split into
        y <- data.frame(matrix(, ncol = (length(id) * 4) + 1, nrow = length(id)))
        y[,1] <- id
        colnames(y) <- c("ID", "Condition1M1", "Condition1M2", "Condition1M3", "Condition1M4", "Condition2M1", "Condition2M2", "Condition2M3", "Condition2M4")
        x1 <- x[which(x[,2] == con[1]),]
        x2 <- x[which(x[,2] == con[2]),]
        #specify sample size
        n <- 5
        x11 <- x1[sample(nrow(x1), n, replace = FALSE),]
        x12 <- x1[sample(nrow(x1), n, replace = FALSE),]
        x13 <- x1[sample(nrow(x1), n, replace = FALSE),]
        x14 <- x1[sample(nrow(x1), n, replace = FALSE),]
        x21 <- x2[sample(nrow(x2), n, replace = FALSE),]
        x22 <- x2[sample(nrow(x2), n, replace = FALSE),]
        x23 <- x2[sample(nrow(x2), n, replace = FALSE),]
        x24 <- x2[sample(nrow(x2), n, replace = FALSE),]

        y[1,2] <- mean(x11[which(x11[,1] == id[1]),3])
        y[1,3] <- mean(x12[which(x12[,1] == id[1]),3])
        y[1,4] <- mean(x13[which(x13[,1] == id[1]),3])
        y[1,5] <- mean(x14[which(x14[,1] == id[1]),3])
        y[2,2] <- mean(x21[which(x21[,1] == id[2]),3])
        y[2,3] <- mean(x22[which(x22[,1] == id[2]),3])
        y[2,4] <- mean(x23[which(x23[,1] == id[2]),3])
        y[2,5] <- mean(x24[which(x24[,1] == id[2]),3])
        y[1,6] <- mean(x11[which(x11[,1] == id[1]),3])
        y[1,7] <- mean(x12[which(x12[,1] == id[1]),3])
        y[1,8] <- mean(x13[which(x13[,1] == id[1]),3])
        y[1,9] <- mean(x14[which(x14[,1] == id[1]),3])
        y[2,6] <- mean(x21[which(x21[,1] == id[2]),3])
        y[2,7] <- mean(x22[which(x22[,1] == id[2]),3])
        y[2,8] <- mean(x23[which(x23[,1] == id[2]),3])
        y[2,9] <- mean(x24[which(x24[,1] == id[2]),3])


        Results:



          ID Condition1M1 Condition1M2 Condition1M3 Condition1M4 Condition2M1 Condition2M2 Condition2M3 Condition2M4
        1 1 133.3333 100 150 133.3333 133.3333 100 150 133.3333
        2 2 125.0000 125 125 120.0000 125.0000 125 125 120.0000






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 18:10









        Alexandra Thayer

        416




        416






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53193673%2fusing-tidyverse-to-randomly-get-means-from-conditions%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud