我从第13页的“The R Inferno”复制粘贴示例,但它无效。
这是以更有效的方式增长data.frame。这是代码:
inferno <- function(n) {
current.N <- 10 * n
my.df <- data.frame(a=character(current.N),
b=numeric(current.N))
count <- 0
for(i in 1:n) {
this.N <- rpois(1, 10)
if(count + this.N > current.N) {
old.df <- my.df
current.N <- round(1.5 * (current.N + this.N))
my.df <- data.frame(a=character(current.N),
b=numeric(current.N))
my.df[1:count,] <- old.df[1:count, ]
}
my.df[count + 1:this.N,] <- data.frame(a=sample(letters,
this.N, replace=TRUE), b=runif(this.N))
count <- count + this.N
}
my.df <- my.df[1:count,]
my.df
}
我收到:[<-.factor
(*tmp*
,iseq,value = c(“6”,“1”,“9”,“4”,“9”,:
无效因子水平,NA生成
怎么了?