greatest.possible.newbie
2012-May-18 11:50 UTC
[R] Problem when index is given by a changing formula in loop - BUG??
Dear community,
I have a little problem filling in a vector with loop output.
I think the problem is the following: I am caluclating the index which
indicates where the loop output should be placed at by a formula. But when I
want to fill in or read out some specific index places this doesn't work
properly. Let me show you my code:
# CODE
################
ln_0 <- 0
ln_max <- 20
ln_schritt <- 0.1
l_frak_0 <- 25
g_l_frak_0 <- 0.5
ln_auslastung <- 2
l_frak <- rep(0, 1+(ln_max-ln_0)/ln_schritt)
ln <- ln_0
while (ln < (ln_max+ln_schritt))
{
insert <- 1+ (ln-ln_0)/ln_schritt
l_frak[insert] <- 100
#l_frak_0 / g_l_frak_0 + floor(ln/ln_auslastung) * l_frak_0/g_l_frak_0 *
(1-g_l_frak_0) - l_frak_0 * (1-g_l_frak_0) * ln
# for simplification I replace the real formula by 100
ln <- ln + ln_schritt
}
names(l_frak) <- seq(ln_0, ln_max, ln_schritt)
#names(l_frak) <- c(1:length(l_frak))
l_frak[which(l_frak[]==0)]
# 0.7 1.2 18.9
# 0 0 0
# The places in the vector where the filling in doesn't work:
# 8 13 190
# Now try the following:
#################
ln <- 0.7
insert <- 1+ (ln-ln_0)/ln_schritt
insert
# [1] 8
# BUT:
l_frak[insert]
# 0.6
# 10
# But it should yield
l_frak[8]
# 0.7
# 0
#########
# END CODE
When I change the mode of the variable "insert" to numeric this
doesn't
change anything. When I change it to
as.numeric(as.character(1+ (ln-ln_0)/ln_schritt))
Then the place where the filling in doesn't work is at
9.9 or at the 100th
place of the vector.
Is this a bug??
Thank you for your help,
Daniel
--
View this message in context:
http://r.789695.n4.nabble.com/Problem-when-index-is-given-by-a-changing-formula-in-loop-BUG-tp4630492.html
Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-May-18 12:54 UTC
[R] Problem when index is given by a changing formula in loop - BUG??
No it's not a bug in R. Indexing can only occur by integers so there's an automatic conversion to integer inside of `[`. Your "insert" variable is actually slightly less than 8 (note as.integer(insert) == 7) by some amount that's too small to print, but the computer still picks up on. More concretely: x <- 1:10 x[2] x[3] x[2.2] x[2.8] x[2.999999] x[insert] x[as.integer(insert)] x[round(insert)] Hopefully that helps clear things up, Michael On Fri, May 18, 2012 at 7:50 AM, greatest.possible.newbie <daniel.hoop at gmx.net> wrote:> Dear community, > > I have a little problem filling in a vector with loop output. > I think the problem is the following: I am caluclating the index which > indicates where the loop output should be placed at by a formula. But when I > want to fill in or read out some specific index places this doesn't work > properly. Let me show you my code: > > # CODE > ################ > ln_0 <- 0 > ln_max <- 20 > ln_schritt <- 0.1 > l_frak_0 <- 25 > g_l_frak_0 <- 0.5 > ln_auslastung <- 2 > > l_frak <- rep(0, 1+(ln_max-ln_0)/ln_schritt) > ln <- ln_0 > > while (ln < (ln_max+ln_schritt)) > { > ?insert <- 1+ (ln-ln_0)/ln_schritt > ?l_frak[insert] <- ?100 > #l_frak_0 / g_l_frak_0 + floor(ln/ln_auslastung) * l_frak_0/g_l_frak_0 * > (1-g_l_frak_0) ?- ?l_frak_0 * (1-g_l_frak_0) * ln > # for simplification I replace the real formula by 100 > > ?ln <- ln + ln_schritt > } > names(l_frak) <- seq(ln_0, ln_max, ln_schritt) > #names(l_frak) <- c(1:length(l_frak)) > l_frak[which(l_frak[]==0)] > # 0.7 ?1.2 18.9 > # ?0 ? ?0 ? ?0 > # The places in the vector where the filling in doesn't work: > # 8 ? 13 ? 190 > > # Now try the following: > ################# > ln <- 0.7 > insert <- 1+ (ln-ln_0)/ln_schritt > insert > # [1] 8 > # BUT: > l_frak[insert] > # 0.6 > # ?10 > # But it should yield > l_frak[8] > # 0.7 > # ? 0 > ######### > # END CODE > > When I change the mode of the variable "insert" to numeric this doesn't > change anything. When I change it to > as.numeric(as.character(1+ (ln-ln_0)/ln_schritt)) > Then the place where the filling in doesn't work is at > 9.9 or at the 100th > place of the vector. > > Is this a bug?? > > Thank you for your help, > Daniel > > > -- > View this message in context: http://r.789695.n4.nabble.com/Problem-when-index-is-given-by-a-changing-formula-in-loop-BUG-tp4630492.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.