Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dataframe

r

I found how to initialize an empty data frame with 3 or 4 dimensions. It's like

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

However, What's the most effective way to initialize an empty data.frame with a lot of column names. like

mynames <- paste("hello", c(1:10000)) 

The wrong way I tried is:

df <- data.frame(mynames=numeric()) 

Thanks a lot beforehand

like image 399
Wilmer E. Henao Avatar asked Dec 10 '13 17:12

Wilmer E. Henao


People also ask

How do I create an empty Dataframe with columns in R?

One simple approach to creating an empty DataFrame in the R programming language is by using data. frame() method without any params. This creates an R DataFrame without rows and columns (0 rows and 0 columns).

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.

Can you create an empty Dataframe in R?

An empty data frame can also be created with or without specifying the column names and column types to the data values contained within it. data. frame() method can be used to create a data frame, and we can assign the column with the empty vectors.

How do you initialize a Dataframe in R with column names?

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.


1 Answers

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)))

like image 65
TheComeOnMan Avatar answered Oct 17 '22 21:10

TheComeOnMan