The data I have collected has 6 different buckets all part of the same set ("Set") filled with random number of marbles ("Marbles") There are 2 treatments, "Color" (Blue/White) and "Size" (Small/Large). Each trial was done in different "Blocks" (ie. time slot). The 6 buckets were all part of one set These buckets are arranged in a circular fashion ("Position"). Here is a sample of the data.
Colour Set Size Position Marbles Block
Blue 1 Small 1 8 1
Blue 1 Small 2 81 1
Blue 1 Small 3 3 1
Blue 1 Small 4 4 1
Blue 1 Small 5 5 1
Blue 1 Small 6 7 1
Blue 6 Small 1 14 2
Blue 6 Small 2 11 2
Blue 6 Small 3 12 2
Blue 6 Small 4 25 2
Blue 6 Small 5 14 2
Blue 6 Small 6 55 2
Blue 1 Large 1 86 1
Blue 1 Large 2 77 1
Blue 1 Large 3 9 1
Blue 1 Large 4 0 1
Blue 1 Large 5 0 1
Blue 1 Large 6 1 1
Blue 1 Large 1 3 1
Blue 1 Large 2 43 1
Blue 1 Large 3 55 1
Blue 1 Large 4 76 1
Blue 1 Large 5 77 1
Blue 1 Large 6 81 1
White 1 Small 1 8 1
White 1 Small 2 81 1
White 1 Small 3 3 1
White 1 Small 4 4 1
White 1 Small 5 5 1
White 1 Small 6 7 1
White 6 Small 1 14 2
White 6 Small 2 11 2
White 6 Small 3 12 2
White 6 Small 4 25 2
White 6 Small 5 14 2
White 6 Small 6 55 2
White 2 Large 1 86 1
White 2 Large 2 77 1
White 2 Large 3 9 1
White 2 Large 4 0 1
White 2 Large 5 0 1
White 2 Large 6 1 1
White 1 Large 1 3 11
White 1 Large 2 43 11
White 1 Large 3 55 11
White 1 Large 4 76 11
White 1 Large 5 77 11
White 1 Large 6 81 11
I want to analyse the data such that across the five replicates, the buckets with the highest number of marbles (hence called the focal bucket) are grouped together and stored in a vector pos1val and the index of the values in position1
pos1 <- vector()
position1 <- vector()
result_df <- dat %>%
group_by(colour, size, block, set) %>%
mutate(result = (marbles == max(marbles)))
result_vec <- which(result_df$result)
pos1val <- result_df[result_df$result =="TRUE","marbles"]
position1 <- which(result_df$result == "TRUE")
I'm not getting the same vector length of pos1val and position1
Next, either the left or the right adjacent bucket will have certain marbles filled. Depending on which of these is the higher, I would like the values store it in vector pos2val and the index of the values in position2
pos2val <- vector()
position2 <- vector()
if(result_df$result == "TRUE" && result_df$pos == "1")
{pos2val <- max(result_df$marbles[pos1+1],result_df$marbles[pos1+5])} #Position 1, 6
if(result_df$result == "TRUE" && result_df$pos == "6")
{pos2val <- max(result_df$marbles[pos1-1],result_df$marbles[pos1-5])} #Position 5, 1
if(result_df$result == "TRUE" && result_df$pos == c("2","3","4","5"))
{pos2val <- max(result_df$marbles[pos1-1], result_df$marbles[pos1+1]} #Position above or below the focal number position1
position2 <- which(pos2val)
I only get NA when I run this code.
Any suggestions on how I may correct this code?
Aucun commentaire:
Enregistrer un commentaire