Creating Data Frames

Last updated: August 19, 2020

Manually creating a data frame

Sometimes it is helpful to manually create a data frame from within R, either for quick testing or for requesting help in a context where creating a short, self-contained example is necessary. This can be done simply with the tribble function, which is for row-wise creation of tibbles.

df <- tribble(
  ~col_name_1, ~col_name_2, ~col_name_3,
  "a", 1, 3,
  "b", 2, 2,
  "c", 3, 1

#  A tibble: 3 x 3
#   col_name_1 col_name_2 col_name_3
#   <chr>           <dbl>      <dbl>
# 1 a                   1          3
# 2 b                   2          2
# 3 c                   3          1

Empty data frames

Another sometimes-useful pattern is creating an empty data frame and then adding rows. I use this pattern for collecting results from a for loop, for example.

df_results <- tribble(~label, ~ci_lower, ~ci_upper)

df_results <- df_results %>% add_row(label="group1", ci_lower=2.7, ci_upper=3.4)
df_results <- df_results %>% add_row(label="group2", ci_lower=1.8, ci_upper=3.7)

#  A tibble: 2 x 3
#   label  ci_lower ci_upper
#   <chr>     <dbl>    <dbl>
# 1 group1      2.7      3.4
# 2 group2      1.8      3.7

When using this pattern, it’s not necessary to pre-specify variable types in the tibble.

ℹī¸ This page is part of my knowledge base for R, the popular statistical programming language. I attempt to use idiomatic practices with the tidyverse collection of packages as much as possible. If you have suggestions for ways to improve this code, please contact me or use the survey link below..