R中的For循环将不会初始化/为i分配适当的值

时间:2018-08-08 10:36:44

标签: 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)

我试图计算每个变量的相对差,使得: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)
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我们可以在seq()循环中将by = 2for一起使用来遍历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]        
}