Selecting multiple subsets of data from a single dataframe column to calculate averages

Hi, I have a set of data which is in a single column sorted on 2 variables (Bio which has variables from 1-36) and then Rep of which there are Rep 1,2 and 3 for each Bio number. I need an average of the different Reps for each Bio number and then to collate it as a datatable (which I will then turn into a bar graph). I have written out the code below which if I write out the first section and specify for each Bio variable from 1:36 it will work - I'm asking if anybody knows of any ways to make the coding more concise as this is a test for working with data sets with a lot more variables?

Ideally when I get to the end section it won't write the Rep 1, 2 and 3 in a continuous column but in 3 separate columns next to each other as well - but I can't get it to do that?

N1 <- (N[N$Bio=="1",])
N1$N1 <-paste(N1$'2525.41')
N1$ID <- 1:nrow(N1)
N1 <- subset(N1,select=c(N1,Rep,ID))

N2 <- (N[N$Bio=="2",])
N2$N2 <-paste(N2$'2525.41')
N2$ID <- 1:nrow(N2)
N2 <- subset(N2,select=c(N2,Rep,ID))

N3 <- (N[N$Bio=="3",])
N3$N3 <-paste(N3$'2525.41')
N3$ID <- 1:nrow(N3)
N3 <- subset(N3,select=c(N3,Rep,ID))

N4 <- (N[N$Bio=="4",])
N4$N4 <-paste(N4$'2525.41')
N4$ID <- 1:nrow(N3)
N4 <- subset(N4,select=c(N4,Rep,ID))

N_merge <- list(N1,N2,N3,N4)
N_merge %>% reduce(full_join,by="ID")
N_merge <- as.data.frame(N_merge)
N_merge <- subset(N_merge,select=c(N1,N2,N3,N4,Rep))

N_merge1 <-(N_merge[N_merge$Rep=="R1",])
N_merge1$ID <- 1:nrow(N_merge1)

N_merge2 <-(N_merge[N_merge$Rep=="R1",])
N_merge2$ID <- 1:nrow(N_merge2)

N_merge3 <-(N_merge[N_merge$Rep=="R1",])
N_merge3$ID <- 1:nrow(N_merge3)

N_merge_Rep <- merge(N_merge1,merge(N_merge2,N_merge3,by="ID"),by="ID")
N_merge_Rep <- as.data.frame(sapply(N_merge_Rep,as.numeric))
Average <- as.data.frame(apply(N_merge_Rep,2,mean))
Average$N1 <-paste(Average$`apply(N_merge_Rep, 2, mean)`)

See the FAQ: How to do a minimal reproducible example reprex for beginners to attract more answers. The friction of having to reverse engineer a problem without data deters answers.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.