How to Initialize Empty Data Frame (Lot of Columns at the Same Time) in R

How to initialize empty data frame (lot of columns at the same time) in R

Maybe this -

df <- data.frame(matrix(ncol = 10000, nrow = 0))
colnames(df) <- paste0("hello", c(1:10000))

And @joran's suggestion - df <- setNames(data.frame(matrix(ncol = 10000, nrow = 0)),paste0("hello", c(1:10000)))

Create empty data frame with column names by assigning a string vector?

How about:

df <- data.frame(matrix(ncol = 3, nrow = 0))
x <- c("name", "age", "gender")
colnames(df) <- x

To do all these operations in one-liner:

setNames(data.frame(matrix(ncol = 3, nrow = 0)), c("name", "age", "gender"))

#[1] name age gender
#<0 rows> (or 0-length row.names)

Or

data.frame(matrix(ncol=3,nrow=0, dimnames=list(NULL, c("name", "age", "gender"))))

How to initialize empty data frame where number of columns is depends on multiple input parameters in R

You can use :

n = 2
m = 12

const_col <- c('name', 'ID', 'nickname')
sc_cols <- c(t(outer(c('start', 'count'), seq_len(n),paste0)))
vm_cols <- c(t(outer(seq_len(n), seq_len(m), function(x, y)
sprintf('value_%d_month_%d', x, y))))
all_cols <- c(const_col, sc_cols, vm_cols)

NewTable <- data.table::data.table(matrix(ncol = length(all_cols),
dimnames = list(NULL, all_cols)))

Specify nrow as 0 if you want an empty data.table with 0 rows.

names(NewTable)
# [1] "name" "ID" "nickname"
# [4] "start1" "start2" "count1"
# [7] "count2" "value_1_month_1" "value_1_month_2"
#[10] "value_1_month_3" "value_1_month_4" "value_1_month_5"
#[13] "value_1_month_6" "value_1_month_7" "value_1_month_8"
#[16] "value_1_month_9" "value_1_month_10" "value_1_month_11"
#[19] "value_1_month_12" "value_2_month_1" "value_2_month_2"
#[22] "value_2_month_3" "value_2_month_4" "value_2_month_5"
#[25] "value_2_month_6" "value_2_month_7" "value_2_month_8"
#[28] "value_2_month_9" "value_2_month_10" "value_2_month_11"
#[31] "value_2_month_12"

Create an empty data.frame

Just initialize it with empty vectors:

df <- data.frame(Date=as.Date(character()),
File=character(),
User=character(),
stringsAsFactors=FALSE)

Here's an other example with different column types :

df <- data.frame(Doubles=double(),
Ints=integer(),
Factors=factor(),
Logicals=logical(),
Characters=character(),
stringsAsFactors=FALSE)

str(df)
> str(df)
'data.frame': 0 obs. of 5 variables:
$ Doubles : num
$ Ints : int
$ Factors : Factor w/ 0 levels:
$ Logicals : logi
$ Characters: chr

N.B. :

Initializing a data.frame with an empty column of the wrong type does not prevent further additions of rows having columns of different types.

This method is just a bit safer in the sense that you'll have the correct column types from the beginning, hence if your code relies on some column type checking, it will work even with a data.frame with zero rows.

Initializing a data frame

Here are four possibilities (I'm sure there are also others):

> data.frame(first=numeric(), second=numeric(), third=numeric(), fourth=numeric())
[1] first second third fourth
<0 rows> (or 0-length row.names)

> data.frame(first=1,second=1,third=1,fourth=1)[0,]
[1] first second third fourth
<0 rows> (or 0-length row.names)

> as.data.frame(matrix(nrow=0,ncol=4,dimnames=list(c(),c("first","second","third","fourth"))))
[1] first second third fourth
<0 rows> (or 0-length row.names)

> setNames(as.data.frame(matrix(nrow=0,ncol=4)), c("first","second","third","fourth"))
[1] first second third fourth
<0 rows> (or 0-length row.names)

Note that for the first solution, you can specify whatever column classes you want (e.g., replacing numeric() with character(), etc.).

Also, you can't specify the dim attribute of a data.frame because data.frames do not have a dim attribute. Rather, they are a list structure with a row.names attribute. The str function can be helpful for understanding what these objects are.

R - How can I create an empty dataframe after transposing?

You subset the empty dataframe. If the dataframe with headers is called df to create 18000 rows with NA values you can do -

out <- df[1:18000, ]
rownames(out) <- NULL
out

R define dimensions of empty data frame

Just create a data frame of empty vectors:

collect1 <- data.frame(id = character(0), max1 = numeric(0), max2 = numeric(0))

But if you know how many rows you're going to have in advance, you should just create the data frame with that many rows to start with.



Related Topics



Leave a reply



Submit