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:
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:
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
add a comment |
up vote
0
down vote
favorite
I have a long format data frame that looks like this:
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:
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
How big is your original data frame?
– Harro Cyranka
Nov 7 at 17:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a long format data frame that looks like this:
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:
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
I have a long format data frame that looks like this:
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:
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
r
asked Nov 7 at 16:26
user2917781
575
575
How big is your original data frame?
– Harro Cyranka
Nov 7 at 17:04
add a comment |
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
add a comment |
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
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 7 at 18:10
Alexandra Thayer
416
416
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
How big is your original data frame?
– Harro Cyranka
Nov 7 at 17:04