Skip to content
Prev 395365 / 398502 Next

How to Reformat a dataframe

You are not getting the structure you want because the indexes are
wrong. They should be something more like this:

i <- 0
for (row in 1:nrow(alajuela_df)){
  for (col in 1:ncol(alajuela_df)){
    i <- i + 1
    df[i,1]=alajuela_df[row,col]
  }
}

but I think what you are doing can be written much shorter and will run faster:

## transpose here matches your original code
df <- data.frame(aportes_alajuela = c(t(alajuela_df)))

## but if you do not want to transpose, then do this
df <- data.frame(aportes_alajuela = unlist(alajuela_df, use.names = FALSE))

However, you said you expected 1509 observations, but this gives you
1512 observations. If you want to exclude the 3 NA observations, do
something like:

df <- df[!is.na(df$aportes_alajuela), , drop = FALSE]
On Fri, Oct 27, 2023 at 11:14?PM Paul Bernal <paulbernal07 at gmail.com> wrote: