Tathagata
Tathagata

Reputation: 2065

Create empty dataframe in R with same columns

 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?

Upvotes: 37

Views: 36064

Answers (4)

clp
clp

Reputation: 1528

For completeness. This resets an existing data frame to zero rows.

U1.RN <- U1
attributes(U1.RN)$row.names <- c()
# <0 rows> (or 0-length row.names)

Note that rownames() <- NULL "deletes" the rownames and then fills them in with the default.

Upvotes: 0

Uwe Mayer
Uwe Mayer

Reputation: 794

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

 df[FALSE,]

Upvotes: 12

joemienko
joemienko

Reputation: 2270

Using dplyr, there are a few good options:

slice(U1, 0)
filter(U1, FALSE)
filter(U1, NA)

The slice approach is probably clearest.

Upvotes: 8

Joshua Ulrich
Joshua Ulrich

Reputation: 176648

You can do this:

U1.RN <- U1[0,]

Upvotes: 93

Related Questions