R program to calculate the mean of the results of the same experiments

Beertje

I am working R program that needs to take the mean values of all the results of the same experiment. For instance, there are two experiments, respectively experiment 1 and experiment 2. Experiment 1 has three results per row, and experiment 2 has two results per row. The program should calculate the mean results of experiment 1 and the mean results of experiment 2.

cols<-c('experiment 1 result 1','experiment 1 result 2','experiment 1 result 3','experiment 2 result 1','experiment 2 result 2') 
df <- data.frame(matrix(ncol = 5, nrow = 1)) 
colnames(df)<-cols 
df[1,]<-c(1,3,2,2,4)

In the case of the given example the output should be the following dataframe:

cols<-c('experiment 1','experiment 2') 
df <- data.frame(matrix(ncol = 2, nrow = 1)) 
colnames(df)<-cols 
df[1,]<-c(2,3)

Depend on the situation the number of experiments, and the number of results per experiment can vary. I am, therefore looking for a generic approach to solve this. Is there somebody who could help me with this?

Thank you in advance.

Ronak Shah

Keep only the "experiment" number from the column names :

sub(' result \\d+', '', names(df))
#[1] "experiment 1" "experiment 1" "experiment 1" "experiment 2" "experiment 2"

Use this as a grouping variable in tapply to get :

tapply(unlist(df), sub(' result \\d+', '', names(df)), mean)
#experiment 1 experiment 2 
#           2            3 

For more than 1 row we can use split.default :

sapply(split.default(df, sub(' result \\d+', '', names(df))), rowMeans)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Loop through multiple folder and Calculate mean of raster of Same folder R

From Dev

R - Calculate mean of rasterbrick

From Dev

Calculate the mean values if the numbers are same

From Dev

A program to calculate the mean and shows the inputs and workings out

From Dev

R: calculate the row mean in a matrix

From Dev

Calculate a lag or lead mean in r

From Dev

Calculate min, maximum and mean in R

From Dev

Calculate a mean, by a condition, within a factor [r]

From Dev

in R, how to calculate mean of all column, by group?

From Dev

How to calculate mean of two timestamp columns in R?

From Dev

for loop in R to calculate mean from list

From Dev

Calculate Mean of Multiply Columns with Condition in R

From Dev

calculate logarithm and mean, add the result to dataset in R

From Dev

Calculate mean based on marker in a second column in R

From Dev

r search along a vector and calculate the mean

From Dev

Calculate the mean value of symmetry data in a matrix with R

From Dev

calculate grand mean from means in r

From Dev

How to calculate mean points for elements in a List in R

From Dev

Nested loops in R to calculate a mean day

From Dev

Calculate daily mean of data frame in r

From Dev

Calculate mean of dataset in R that satisfies two conditions

From Dev

Calculate the mean of a row excluding certain columns in R

From Dev

How to calculate mean for all pairwise combinations in R

From Dev

How To Calculate Mean In R (Data Structure)

From Dev

R:how to calculate poblational standard deviation and mean

From Dev

How to Calculate eCDF Mean in MatchIt() R

From Dev

Running a for loop in r over columns to calculate mean

From Dev

R Loop to calculate mean for each of two variables

From Dev

Using 'boot' to calculate bootstrapped mean in R

Related Related

  1. 1

    Loop through multiple folder and Calculate mean of raster of Same folder R

  2. 2

    R - Calculate mean of rasterbrick

  3. 3

    Calculate the mean values if the numbers are same

  4. 4

    A program to calculate the mean and shows the inputs and workings out

  5. 5

    R: calculate the row mean in a matrix

  6. 6

    Calculate a lag or lead mean in r

  7. 7

    Calculate min, maximum and mean in R

  8. 8

    Calculate a mean, by a condition, within a factor [r]

  9. 9

    in R, how to calculate mean of all column, by group?

  10. 10

    How to calculate mean of two timestamp columns in R?

  11. 11

    for loop in R to calculate mean from list

  12. 12

    Calculate Mean of Multiply Columns with Condition in R

  13. 13

    calculate logarithm and mean, add the result to dataset in R

  14. 14

    Calculate mean based on marker in a second column in R

  15. 15

    r search along a vector and calculate the mean

  16. 16

    Calculate the mean value of symmetry data in a matrix with R

  17. 17

    calculate grand mean from means in r

  18. 18

    How to calculate mean points for elements in a List in R

  19. 19

    Nested loops in R to calculate a mean day

  20. 20

    Calculate daily mean of data frame in r

  21. 21

    Calculate mean of dataset in R that satisfies two conditions

  22. 22

    Calculate the mean of a row excluding certain columns in R

  23. 23

    How to calculate mean for all pairwise combinations in R

  24. 24

    How To Calculate Mean In R (Data Structure)

  25. 25

    R:how to calculate poblational standard deviation and mean

  26. 26

    How to Calculate eCDF Mean in MatchIt() R

  27. 27

    Running a for loop in r over columns to calculate mean

  28. 28

    R Loop to calculate mean for each of two variables

  29. 29

    Using 'boot' to calculate bootstrapped mean in R

HotTag

Archive