Efficiently creating a data frame that is made up of smaller data frames

erickawaguchi
Sat, 10 Sep 2022 22:54:13 +0000
Modelling & Simulations

Hi all,

I’m trying to create a “long” version data frame (e.g. several observations per individual as in a longitudinal study). However, the data frame for each individual will depend on certain parameters - not the same operation for all N individuals; some will have more rows, some fewer. I was able to do it by creating a blank data frame and then continually updating it within a for loop using vcat.

In terms of pseudocode, this is how it currently looks:

df = DataFrame(ID = Int64[], nᵢ = Int64[], ...) # Define the actual data frame I want

for i in 1:N
    dftmp = DataFrame(ID = i, nᵢ = Int64[], ...) # Create a temporary data frame (same args as df)
    .
    .
    .
    df = vcat(df, dftmp) # Combine dftmp with df (i.e. Update df)
end

However, I feel like there must be a more efficient way of doing this and wanted guidance. I come from an R background and have manipulated data frames using tidyverse.

Thanks in advance.

6 posts - 3 participants