Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create empty dataframe in R with same columns

Tags:

r

 names(U1)  [1] "username"     "review_count" "forum_posts"  "age"          "avg_interval" [6] "avg_sim"      "class" 

So how do I create an empty data frame U1.RN that will have same columns as U1?

like image 767
Tathagata Avatar asked Nov 24 '10 16:11

Tathagata


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 create a null 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.

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.


2 Answers

You can do this:

U1.RN <- U1[0,] 
like image 187
Joshua Ulrich Avatar answered Oct 21 '22 22:10

Joshua Ulrich


Along the lines of df[0,] you can also use a boolean mask which might make the code more readable:

 df[FALSE,] 
like image 31
Uwe Mayer Avatar answered Oct 21 '22 22:10

Uwe Mayer