感谢我从阅读内容中获得的所有帮助。
当我只处理一个data.frame时,我对我的R循环不满意,因为我必须一遍又一遍地写下数据帧的名称,这会使我的R代码膨胀。
这是一个愚蠢的例子:
x<- rep(NA,10) y <- 1:10 dat <- data.frame(x,y) for(i in 2:nrow(dat)){ dat$x[i] <- dat$y[i] + dat$y[i-1] }所以我想要摆脱的是dat$ -bit。 外部循环这可以用in within()来完成,但我不确定你是否可以用R实际做到这一点。我试过它:
remove(x,y) # In order to avoid accidental usage of the initial vectors within(dat,{ for(i in 2:nrow(dat)){ x[i] <- y[i] + y[i-1] }})输出如下:
x y i 1 NA 1 10 2 3 2 10 3 5 3 10 4 7 4 10 5 9 5 10 6 11 6 10 7 13 7 10 8 15 8 10 9 17 9 10 10 19 10 10所以循环确实有效,只是有一个新的神奇专栏。
有没有人知道(1)这里发生了什么,以及(2)如何优雅地处理这种循环(一个更复杂的例子包裹within()一个循环within()包括几个if()语句和计算失败btw?
非常感谢提前! SKR
Thanks for all the help I got from just reading stuff.
I'm not happy with my R loops when I am only dealing within one data.frame because I have to write down the name of the dataframe over and over again which bloats up my R code.
Here is a silly example:
x<- rep(NA,10) y <- 1:10 dat <- data.frame(x,y) for(i in 2:nrow(dat)){ dat$x[i] <- dat$y[i] + dat$y[i-1] }So what I want to get rid of is that dat$ -bit. Outside loops this can neatly be done with within(), but I am not exactly sure whether you can actually do that with R. I tried it though:
remove(x,y) # In order to avoid accidental usage of the initial vectors within(dat,{ for(i in 2:nrow(dat)){ x[i] <- y[i] + y[i-1] }})The output looks like this:
x y i 1 NA 1 10 2 3 2 10 3 5 3 10 4 7 4 10 5 9 5 10 6 11 6 10 7 13 7 10 8 15 8 10 9 17 9 10 10 19 10 10So the loop did actually work, it's just that there is a new magical column.
Does anyone know (1) what is going on here and (2) how to elegantly deal with that kind of loops (a more complicated example wrapping within() around a loop including several if() statements and calculations failed btw?
Thanks a lot in advance! skr
最满意答案
Ben回答了你的主要问题,指出i被for循环分配给了i 。 你可以通过尝试这样的事情来看到这一点:
for(j in 1:3) cat("hi\n") hi hi hi > j [1] 3一种选择是通过使其值为NULL来删除不需要的i变量:
within(dat,{ for(i in 2:nrow(dat)){ x[i] <- y[i] + y[i-1] } i <- NULL })另一种方法是使用with()而不是within() :
dat$x <- with(dat, { for(i in 2:nrow(dat)){ x[i] <- y[i] + y[i-1] } x })最后,虽然我意识到你的是一个玩具的例子,但最好的解决办法通常是完全避免循环:
d <- data.frame(y=1:10) within(d, {x = y + c(NA, head(y, -1))}) # y x # 1 1 NA # 2 2 3 # 3 3 5 # 4 4 7 # 5 5 9 # 6 6 11 # 7 7 13 # 8 8 15 # 9 9 17 # 10 10 19Ben answered your main question, by noting that i is being assigned to by the for loop. You can see that that is so by trying something like this:
for(j in 1:3) cat("hi\n") hi hi hi > j [1] 3One option is just to remove the unwanted i variable by making its value NULL:
within(dat,{ for(i in 2:nrow(dat)){ x[i] <- y[i] + y[i-1] } i <- NULL })Another is to use with() instead of within():
dat$x <- with(dat, { for(i in 2:nrow(dat)){ x[i] <- y[i] + y[i-1] } x })Finally, though I realize yours was a toy example, the best solution will very often be to avoid for loops altogether:
d <- data.frame(y=1:10) within(d, {x = y + c(NA, head(y, -1))}) # y x # 1 1 NA # 2 2 3 # 3 3 5 # 4 4 7 # 5 5 9 # 6 6 11 # 7 7 13 # 8 8 15 # 9 9 17 # 10 10 19更多推荐
发布评论