R中的For循环不会初始化/为i分配正确的值

R中的For循环不会初始化/为i分配正确的值,r,R,假设这是我的初始数据帧的结构random: title<-c(1:10) x1<-c(runif(10)) x2<-c(runif(10)) y1<-c(runif(10)) y2<-c(runif(10)) random<-data.frame(title, x1, x2, y1, y2) 当逐行运行时,我的for循环运行良好,但当运行整个脚本时,它既不会初始化循环,也不会为I指定适当的值;它指定观察值而不是可变值 for (i in random[ ,c

假设这是我的初始数据帧的结构
random

title<-c(1:10)
x1<-c(runif(10))
x2<-c(runif(10))
y1<-c(runif(10))
y2<-c(runif(10))
random<-data.frame(title, x1, x2, y1, y2)
当逐行运行时,我的
for
循环运行良好,但当运行整个脚本时,它既不会初始化循环,也不会为
I
指定适当的值;它指定观察值而不是可变值

for (i in random[ ,c(1, 2*i, 2*i+1)]){

  name1 <- paste("dif_rel_", names(random)[2*i], sep="")
  result <- data.frame(rel_dif=(random[,3]-random[,2])/random[,2])
  names(result) <- c(name1)

  if (i==1){
    analisis_random <- cbind(title=random$title, result)
  }else
    analisis_random <- analisis_random %>%
    cbind(result)
}
for(i随机[,c(1,2*i,2*i+1)]){
名称1
循环:

nb.var <- ncol(random) %/% 2
random.analysis <- data.frame(random$title)
for( i in 1:nb.var ) {
  j <- 2*i
  name <- colnames(random)[j]
  name <- substr(name, 1, length(name))
  random.analysis[[name]] <- (random[, j+1] - random[, j]) / random[, j] 
}
我们可以在
for
循环中使用
seq()
by=2
来迭代data.frame的成对列

nc <- ncol(random)
for (i in seq(from=2, to=nc-1, by=2)) {
  random[paste0("dif_rel_", names(random)[i])] <- (random[i+1]-random[i])/random[i]        
}

nc
random$rel_dif_x什么是
for(i in random[,c(1,2*i,2*i+1)]
应该完成?您在自己的定义中使用了
i
。@KonradRudolph的目标是在循环第1、2*i和(2*i)列时保留所有观察值+1@johnnydoe但是在这个计算中,
i
是什么?在计算这个表达式的时候,
i
还没有被定义。@KonradRudolph我明白你的意思了……我想我是列的数量。我使用的实际数据框有360k个观察值和500个变量……我需要自动化这个过程
   title        x1         x2        y1        y2         z1        z2
1      1 0.7121342 0.29333074 0.6794730 0.2137924 0.21198103 0.7449928
2      2 0.5885867 0.96948469 0.8244739 0.2012238 0.62282812 0.4100822
3      3 0.1157999 0.30372600 0.9212240 0.8259835 0.57565854 0.7912434
4      4 0.3729795 0.62767128 0.6722178 0.6159081 0.09886538 0.0742936
5      5 0.7058853 0.76085048 0.6954550 0.8716693 0.50313245 0.5764264
6      6 0.8249212 0.07457001 0.1529763 0.8033486 0.24885531 0.1529997
7      7 0.9134835 0.14298191 0.8090683 0.7189970 0.53919015 0.7723871
8      8 0.2983176 0.18880266 0.9015305 0.3370120 0.43882282 0.1521721
9      9 0.6579563 0.63984312 0.9350361 0.9302642 0.35204606 0.7087695
10    10 0.4136457 0.42151020 0.1064115 0.4648270 0.48859854 0.7495744
nb.var <- ncol(random) %/% 2
random.analysis <- data.frame(random$title)
for( i in 1:nb.var ) {
  j <- 2*i
  name <- colnames(random)[j]
  name <- substr(name, 1, length(name))
  random.analysis[[name]] <- (random[, j+1] - random[, j]) / random[, j] 
}
random.title           x            y          z
1             1 -0.58809625 -0.685355501  2.5144315
2             2  0.64714012 -0.755936777 -0.3415805
3             3  1.62285258 -0.103384684  0.3745013
4             4  0.68285737 -0.083767116 -0.2485377
5             5  0.07786703  0.253379759  0.1456752
6             6 -0.90960348  4.251458518 -0.3851862
7             7 -0.84347621 -0.111327223  0.4324947
8             8 -0.36710858 -0.626177931 -0.6532265
9             9 -0.02752952 -0.005103397  1.0132863
10           10  0.01901274  3.368201500  0.5341314
nc <- ncol(random)
for (i in seq(from=2, to=nc-1, by=2)) {
  random[paste0("dif_rel_", names(random)[i])] <- (random[i+1]-random[i])/random[i]        
}