[R] Collecting data in a data structure

Richard M. Heiberger rmh at temple.edu
Thu Sep 17 06:49:54 CEST 2009


There are two issues here.

The specifics of the error message are because you declared
a data.frame with 0 columns and then attempted to change two
columns.

 > data.frame()
data frame with 0 columns and 0 rows
 > q <- data.frame()
 > q[1,] <- data.frame(a=3, b=6)
Warning in `[<-.data.frame`(`*tmp*`, 1, , value = list(a = 3, b = 6)) :
   provided 2 variables to replace 0 variables
 >


The second issue is that you need to read about logical subscripting.
  ?`[`


Assuming your function is.prime is vectorized, meaning
 > is.prime(1:10) ## 2 3 5 7
[1]  FALSE TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE

then this single statement will do what you want

qq <- d[is.prime(d[,1]),]

I recommend you not use "q" as a variable name.
It will cause you trouble later when you confuse it with q() for
quitting an R session.

Rich




More information about the R-help mailing list