首页 > 解决方案 > R中的for循环不会为i初始化/分配正确的值

问题描述

假设这是我的初始数据框的结构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)

我正在尝试计算每个变量的相对差异,例如rel_dif_x = (x2 - x1)/x1

我正在尝试使用for循环来执行此操作,这将打印一个analisis_random具有以下结构的新数据框:

> str(analisis_random)
'data.frame':   10 obs. of  3 variables:
 $ title:
 $dif_rel_x:
 $dif_rel_y:

我的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)
}

标签: r

解决方案


set.seed(20180808)
# needed for reproductible example

title<-c(1:10)

x1<-c(runif(10))
x2<-c(runif(10))

y1<-c(runif(10))
y2<-c(runif(10))

z1<-c(runif(10))
z2<-c(runif(10))

random<-data.frame(title, x1, x2, y1, y2, z1, z2)

这将导致 :

   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] 
}

OP询问的结果:

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

推荐阅读