【发布时间】:2012-01-03 10:04:32
【问题描述】:
我想知道是否有任何方法可以有效地解决以下问题。我有一个 X-Y 点的集合。对于每个点,我需要生成一定数量的记录,最后,我需要将所有正在生成的记录堆叠在一起。最初,我使用 FOR 循环并在每个循环中使用 cbind 堆栈 data.frame 来执行此操作。现在通过定义最终记录堆栈的尺寸对其进行了一些更改,并尝试用生成的值替换那些 0。我的代码发布在下面(带有**,我指出了我被卡住的地方)..如果你能给我一个提示,或者甚至有更好的解决方案,那将是完美的!
colonies <- read.table(text =
' X Y Timecount ID_col Age
582906.4 2883317 2004 1 15
583345.9 2883102 2004 2 4
583119.5 2883621 2004 3 13
583385.0 2882933 2004 4 5
583374.0 2882936 2004 5 2
583271.0 2883076 2004 7 5
582898.9 2883229 2004 8 1
582927.9 2883234 2004 9 20
582956.7 2883272 2004 10 13
582958.8 2883249 2004 11 3', header = TRUE)
year = 2004
survival_prob = 0.01
male_prob = 0.5
Present <- colonies$Timecount == year
app <- sum(colonies$Age[Present] >= 4 & colonies$Age[Present] < 10) * 1000 * survival_prob
app2 <- sum(colonies$Age[Present] >= 10 & colonies$Age[Present] < 15) * 10000 * survival_prob
app3 <- sum(colonies$Age[Present] >= 15 & colonies$Age[Present] <= 20) * 100000 * survival_prob
size <- app + app2 + app3
pop <- data.frame(matrix(0,nrow=size,ncol=2))
colnames(pop) <- c("X","Y")
if (dim(pop)[1] > 0){
#FOR cycle going through each existing point
for (i in 1:sum(Present)){
if (colonies[Present,]$Age[i] < 4) { next
} else if (colonies[Present,]$Age[i] >= 4 & colonies[Present,]$Age[i] < 10) { alates <- 1000
} else if (colonies[Present,]$Age[i] >= 10 & colonies[Present,]$Age[i] < 15) { alates <- 10000
} else if (colonies[Present,]$Age[i] >= 15 & colonies[Present,]$Age[i] <= 20) { alates <- 100000
}
indiv <- alates * survival_prob
#Initialize two coordinate variables based on the established (or existing) colonies
X_temp <- round(colonies[Present,]$X[i],2)
Y_temp <- round(colonies[Present,]$Y[i],2)
distance <- rexp(indiv,rate=1/200)
theta <- runif(indiv, 0, 2*pi)
C <- cos(theta)
S <- sin(theta)
#XY coords (meters) using polar coordinate transformations
X <- X_temp + round(S * distance,2)
Y <- Y_temp + round(C * distance,2)
pop[,] <- c(X,Y) #******HERE I GOT STUCK...it should be pop[1:indiv,]
#but then it does not work for the next i since it would over write...
}
pop$Sex <- rbinom(size,1,male_prob)
pop$ID <- 1:dim(pop)[1]
}
【问题讨论】:
-
代码似乎有问题......你真的不想对 4 岁以下的孩子做任何事吗?如果是这样,请立即扔掉它。在我看来,这一切都可以被矢量化。请更好地评论它,也许更好地描述你想要完成的事情。
标签: r for-loop replace dataframe vectorization