I wrote a loop generating the average and standard deviation of the randomized data set. The randomization size would differ depending on the sample size of each sample. Here I have sample sizes 80, 50, 32, and 20, so I wrote a loop as below.
However, I kept receive the error "Error in CT.raw$average[i, ] <- mean(as.numeric(unlist(as.data.frame(CT.raw[i, : incorrect number of subscripts on matrix"
The tricky thing is, I chose some numbers and plug in as i, the mean and standard deviation would work out fine. I have tried many times but have not yet figured it out where I have been doing wrong.
The sample raw data set is as the link below:
random.f <- function(Data){
x=seq(from=2, to=1730, length=1729)
Data=CT.raw
set.seed(123)
for (i in x){
#for j = 11 to 91
if (is.na(CT.raw[i,90])==FALSE)
{
index = sample(1:80, 35, replace=FALSE)
index <- as.numeric(index)
CT.raw <- as.data.frame(CT.raw)
CT.raw$average[i,]= mean(as.numeric(unlist(as.data.frame(CT.raw[i,11:90])[c(index),])), trim = 0, na.rm = TRUE)
}else if(is.na(CT.raw[i,60])==FALSE & is.na(CT.raw[i,61])==TRUE)
{
index = sample(1:50, 25, replace=FALSE)
index <- as.numeric(index)
CT.raw$average[i,]= mean(as.numeric(unlist(as.data.frame(CT.raw[i, 11:90])[c(index),])), trim=0, na.rm=TRUE)
}else if(is.na(CT.raw[i,42])==FALSE & is.na(CT.raw[i,43])==TRUE)
{
index = sample(1:32, 20, replace=FALSE)
index <- as.numeric(index)
CT.raw$average[i,]= mean(as.numeric(unlist(as.data.frame(CT.raw[i,11:90])[c(index),])), trim=0, na.rm=TRUE)
}else (is.na(CT.raw[i,30])==FALSE & is.na(CT.raw[i,31])==TRUE)
{
index = sample(1:20, 15, replace=FALSE)
index <- as.numeric(index)
CT.raw$average[i,]= mean(as.numeric(unlist(as.data.frame(CT.raw[i,11:90])[c(index),])), trim=0, na.rm=TRUE)
}
}
}