Analysis over time comparing 2 data blocks row by row

This is a small part of the dataframe that I am working with for reference. I am working with a data frame (MG53_HanLab) in R which has a column for Time, several columns named "MG53" in them, several columns named "F2" and several with "Iono" in them. I would like to compare the means of each group for each time point. I understand that I need to multiply the data and tried to do

control <- MG53_HanLab[c(2:11)]
F2 <- MG53_HanLab[c(12:23)]
iono <- MG53_HanLab[c(24:33)]

      

which created 3 new data frames.

My question is, how do I compare two rows of data row by row to see if there is a difference in means for each table?

+3


source to share


2 answers


rowMeans

feels easier as @Chi Pak suggested.



#create example data
time<-seq(1.0,6.0,.5)
A_1<-runif(11)
A_2<-runif(11)
B_1_1<-runif(11)
B_1_2<-runif(11)
B_2<-runif(11)

#create data frame
df<-data.frame(time,A_1,A_2,B_1_1,B_1_2,B_2)

#subset column groups into individual data frames using regular expression
df.a<-df[,grep('A_',colnames(df))]

#calculate rowMeans
a.mean<-rowMeans(df.a)

#repeat for other column groups
df.b<-df[,grep('B_',colnames(df))]
b.mean<-rowMeans(df.b)

#recombine to view side by side
df.mean<-data.frame(a.mean,b.mean)

      

+2


source


You can use a package data.table

with some column-to-row flips and back.



#import library
library(data.table)

#create example data
time<-seq(1.0,6.0,.5)
A_1<-runif(11)
A_2<-runif(11)
B_1_1<-runif(11)
B_1_2<-runif(11)
B_2<-runif(11)

#instantiate data table from example data
dt <-data.table(time,A_1,A_2,B_1_1,B_1_2,B_2)

#flip all columns with underscores in name into rows using regular expression
dt.m = melt(dt,id.vars=c("time"), measure.vars=grep('_',colnames(dt)))

#remove characters after '_' to homogenize column groups
dt.m[,variable:=sub("_.*","",variable)]

#calculate the mean grouped by time and column groups
dt.mean<-dt.m[,lapply(.SD,mean),by=.(time,variable)]

#flip rows back to columns
dcast(dt.mean,time~variable,value.var = "value")

      

+1


source







All Articles