I have a dataframe
with 231 variables (these are blood parameters with the exception of two, these are timepoint the sample was taken and temperature the sample was kept at), my observations differ by patient and the time in hours after taking the sample (8 people and 11 time points, therefore 88 observations)
I would like to perform mutliple t tests looking for whether there is a difference between the blood parameter whether it is kept in the fridge or at room temperature, for each time point
Eg. At time point 1 hour, does the measured cholesterol differ if the sample was kept in the fridge or ambient
This is my attempt so far:
1) I subsetted the data to only have time point 1
Data1 <- Maindata[Maindata$Timepoint =="1",]
2) Second I ran the t.test for a single variable, albumin in this case
t.test(Data1L$Albumin[Data1$Temperature=="Fridge"],Data1$Albumin[Data1$Temperature=="Ambient"], paired = T)
3) Whilst this works, is there a way I can fashion a for loop to run multiple T tests, the two groups for each test will be variable in the fridge or ambient.
Also is there any way to do this without subsetting each time factor?
Thank you for your help.
I have tried to follow the instructions as suggested by the duplicate question
sapply(names(DF)[-ncol(DF)], function(x){
t.test(DF[DF$Temperature=="Fridge", x],
DF[DF$Temperature=="Ambient", x])$p.value
})
However I am still struggling
Error in t.test.default(One[One$Temperature == "Fridge", x], One[One$Temperature == : not enough 'x' observations In addition: Warning messages: 1: In mean.default(x) : argument is not numeric or logical: returning NA 2: In var(x) : Calling var(x) on a factor x is deprecated and will become an error. Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.