Q: How to create a matrix from multiple t.test in r So i have a data frame that im supposed to do a t.test on and in the end have a matrix with the following columns "Lower CI-limit", "Mean", "Upper CI-limit", "no of obs." it is supposed to look something like this when its done, the code i've tried so far is this 
my_grouped_test <- function(data_vector, my_groups, alpha){



  fct <- as.factor(my_groups)
  lvl <- levels(fct)
  tbl <- table(my_groups)

  result <- matrix(0, nrow = length(unique(fct)), ncol = 4)
  colnames(result) <- c("Lower CI-limit", "Mean", "Upper CI-limit", "no of obs.")
  rownames(result) <- c(0:(length(lvl)-1))
  result[,4] <- tbl
  group_test <- by(data_vector, INDICES = fct, FUN = t.test, conf.level=(1-alpha))

  for(i in 1:length(lvl)){
  result[(i),c(1,3)] <- group_test$'i'$conf.int
  result[(i),2] <- group_test$'i'$estimate

  }
  return(result)
}


But the result i get is first and foremost "number of items to replace is not a multiple of replacement length". I am also required to handle the data as seen above like this 
group_test <- by(data_vector, INDICES = fct, FUN = t.test, conf.level=(1-alpha))


A: my_grouped_test <- function(data_vector, my_groups, alpha){



    fct <- as.factor(my_groups)
    lvl <- levels(fct)
    tbl <- table(my_groups)

    result <- matrix(0, nrow = length(unique(fct)), ncol = 4)
    colnames(result) <- c("Lower CI-limit", "Mean", "Upper CI-limit", "no of obs.")
    rownames(result) <- c(0:(length(lvl)-1))
    result[,4] <- tbl
    group_test <- by(data_vector, INDICES = fct, FUN = t.test, conf.level=(1-alpha))

    for(i in 1:length(lvl)){
        result[(i),c(1,3)] <- group_test[[i]]$conf.int
        result[(i),2] <- group_test[[i]]$estimate

    }
    return(result)
}

group_test is a list. To access to the elements of the list you should use [[i]]. To see the object you can use str(group_test) you will see it's a list.
