37
 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?

4
  • 4
    Can I ask why you need a 0-row data frame? Depending on what you are going to do with it, it might be more efficient to do things a different way (e.g. I hope you aren't planning on filling this row by row in a loop?) Commented Nov 24, 2010 at 16:40
  • "e.g. I hope you aren't planning on filling this row by row in a loop?" - yeah, :(. What is the R-y way to do the equiv of [pseudocode] for(i in 1:6000) if (pred.U1.nb.c[i]=='unlabeled') U1.RN[j++,]<-U1[i,] [/pseudocode], where pred.U1.nb.c is a vector I got from a predict(), and want to create a data frame by selecting those rows of U1 that predict spewed out? (... trying hard to be verbose and not confusing simultaneously) Commented Nov 24, 2010 at 16:58
  • 6
    In R, preallocate your storage! You know you want a 6000-row data frame ahead of the loop, so create one and fill it in row by row. Or even quicker; create a matrix of the correct dimension, fill that row by row, and then convert to a data frame, as matrices are much faster to work with. If you want more help (looks like you might not even need a loop, just some simple indexing and subsetting/insertion), can you start a new Q and provide a proper, small example of what you really want to do? If you do, I'll promise to look at it and give a go at an answer. Commented Nov 24, 2010 at 17:18
  • Thanks Gavin, here's the Q: tinyurl.com/26ugewv Commented Nov 24, 2010 at 17:54

4 Answers 4

93

You can do this:

U1.RN <- U1[0,]
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect ..... > U1.RN<-U1[0,] > names(U1.RN) [1] "username" "review_count" "forum_posts" "age" "avg_interval" [6] "avg_sim" "class" > nrow(U1) [1] 6000 > nrow(U1.RN) [1] 0
+1 neat!!!!!!!! (the extra ! were to get round the min character limit, oh, wait, ... ;-)
12

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

 df[FALSE,]

Comments

8

Using dplyr, there are a few good options:

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

The slice approach is probably clearest.

2 Comments

filter(FALSE) also works and is maybe a little clear of intent. slice(0) might be even better.
Agreed that slice(0) seems to be a winner on readability/clarity
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.