Initializing an empty `tibble` and adding columns to it sequentially

In Julia DataFrames, there is a useful feature for adding columns to empty dataframes.

df = DataFrame()
for i in 1:10 
    df[:, "var_$i"] = rand(100)
end

I have the need to do a similar operation in R. I have an 10 x 10 x 5 matrix. Along the third dimension I want to make a column in a tibble that is a vector of length 100. My current solution is

x = array(1, dim = c(10, 10, 5))
t = list()
for(i in 1:5){
    n = paste0("var_", i)
    t[[n]] = c(x[ , , i])
}
as_tibble(t)

Is there a more idiomatic way to do this?

It can be done with a 1 liner I think using purrr package which is part of the tidyverse

purrr::map_dfc(1:5,~as.vector(x[,,.]))
1 Like

Thanks. This is a good solution for this particular problem. Can you help me out in translating this to something more general?

Say I have three arrays

I have a variable suffix I want to append to the name when I construct them into a data frame, such the end result is



x = 1:5
y = 2:6
z = 3:7

suffix = "end"

And as a result I want

# A tibble: 5 x 3
  x_end y_end z_end
  <int> <int> <int>
1     1     2     3
2     2     3     4
3     3     4     5
4     4     5     6
5     5     6     7

Is the best way to do this to

  1. Construct a list like above
  2. Make a tibble and rename the columns
  3. Use purrr::map_dfc in some way I don't fully understand

In this setup, I think the most elegant approach is using tibble and its .name_repair param like so:

tibble(x, y, z , 
       .name_repair = ~ paste0(.,suffix))

You would edit your suffix to be '_end' rather than 'end' to save yourself fiddling with paste's sep param

Thank you! This is I think what I want.

Though I just realized in this particular I can just add the suffix at the end since I am merging a few data frames together.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.