How do I store the output of a repeat loop in a dataframe in R

by Mouk   Last Updated September 13, 2018 23:26 PM

My basic idea is to compute the Means of chunks (column-wise) of a large matrix and store these Means as rows of a data frame. Note, the chunks have different sizes (number of rows) and these are stored in a vector "vec1". Below is my code:

df <- setNames(data.frame(matrix(nrow = 4000, ncol = 3)),
                             c("Age","Weight", "height"))
#
i <- 1
j <- vec1[1] - 1
k <- 0
repeat{
    elements <- as.vector(apply(mydata[i:(j+1),3:5], 2, mean))
    df <- rbind(df, elements)
    k <- k+1
    i = i + vec1[k] 
    j = j + vec1[k+1]
    if (j + 1 >= l){
        break
    }
}
Tags : r dataframe


Related Questions





Pyspark - Get X random records from dataframe

Updated March 31, 2017 15:26 PM