Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Create empty tibble/data frame with column names coming from a vector

I would like to create an empty data frame where the column names are coming from a character vector.

for example, if this was my vector:

 vec <- letters[1:3]

I would like create an empty data frame like the following:

 df <- tibble('a' = character(), 'b' = character(), 'c' = character())

however, I would like to iterate through the items in the vector to populate the dataframe names and not have to manually specify each one. In reality my vector has 40+ names.

I've tried the following by they don't work:

 df <- tibble(vec[1:3])
 df <- tibble(vec)
 df <- tibble(for (i in 1:3){
   vec[i]
 })

Any help on this would be greatly appreciated!

like image 770
steve zissou Avatar asked Oct 17 '19 11:10

steve zissou


People also ask

How do I create an empty DataFrame in R with column names?

To create an empty data frame with column names , initialize a vector with column names first, c() is used to create a Vector in R. And then create DataFrame by using data. frame() function and assign this vector to columns(df) .

How do I create a DataFrame with specific column names in R?

How to Create a Data Frame. We can create a dataframe in R by passing the variable a,b,c,d into the data. frame() function. We can R create dataframe and name the columns with name() and simply specify the name of the variables.

How do I create an empty column in a DataFrame in R?

The easiest way to add an empty column to a dataframe in R is to use the add_column() method: dataf %>% add_column(new_col = NA) . Note, that this includes installing dplyr or tidyverse.

How do I populate an empty DataFrame in R?

To create an empty Data Frame in R, call data. frame() function, and pas no arguments to it. The function returns an empty Data Frame with zero rows and zero columns.


2 Answers

You can create a named vector, vec, where the first argument sets the type of column you want. The rep("", 3) line says I want three character columns. Then the second argument is the vector of column names.

Use dplyr::bind_rows to convert this into tibble with one row. Then [0, ] selects zero rows, leaving it empty.

With this method, you can control the data type for each column easily.

library(dplyr)

vec <- setNames(rep("", 3), letters[1:3])
bind_rows(vec)[0, ]

# A tibble: 0 x 3
# ... with 3 variables: a <chr>, b <chr>, c <chr>

You can also use as_tibble if you transpose the named vector. I guess I use bind_rows because I usually have dplyr loaded but not tibble.

library(tibble)

vec <- setNames(rep("", 3), letters[1:3])
as_tibble(t(vec))[0, ]

# A tibble: 0 x 3
# ... with 3 variables: a <chr>, b <chr>, c <chr>

If you know all of the columns are of a single type (e.g., character), you can do something like this.

vec <- letters[1:3]
df <- bind_rows(setNames(rep("", length(vec)), vec))[0, ]
like image 155
Adam Avatar answered Sep 29 '22 11:09

Adam


Another variant of Adam's idea:

as_tibble(sapply(vec, \(x) character()))

# A tibble: 0 x 3
# ... with 3 variables: a <chr>, b <chr>, c <chr>
like image 25
sindri_baldur Avatar answered Sep 29 '22 11:09

sindri_baldur