[R] remove 0 rows from a data frame
Jason Turner
jasont at indigoindustrial.co.nz
Sun Nov 23 18:34:11 CET 2003
Tord Snall wrote:
> Dear all,
>
> As part of a larger function, I am randomly removing rows from a data
> frame. The number of removed rows is determmined by a Poisson distribution
> with a low mean. Sometimes, the random number is 0, and that's when the
> problem starts:
...
> However, sometimes rpois(1, 2) lead to nft=0, and in that case I do not want
>
>
>>temp2<- temp[-sample(nrow(temp), 0), ]
>>temp2
>
> [1] occ x y dbh age
> <0 rows> (or 0-length row.names)
This is a language feature, not a bug. The easiest way around it is to
catch the case where it's an issue. Something like
if(nft==0) {
rows <- 1:nrow(temp)
} else {
rows <- -1 * sample(1:nrow(temp),nft)
}
temp2 <- temp[rows,]
Cheers
Jason
--
Indigo Industrial Controls Ltd.
http://www.indigoindustrial.co.nz
64-21-343-545
jasont at indigoindustrial.co.nz
More information about the R-help
mailing list