在R中构造For循环和if循环

时间:2019-10-11 01:01:51

标签: r

假设我有一个数据集,其中有600个名为w的观测值。

> w
              w0_7       w1_7
  [1,] -0.03272023 0.66042696
  [2,] -0.08887320 0.60427398
  [3,] -0.03037798 0.66276920
  [4,] -0.06745567 0.62569151
  [5,] -0.04390675 0.64924043
  [6,] -0.10160427 0.59154291
  [7,] -0.05075316 0.64239402
  [8,] -0.02616434 0.66698285
  [9,] -0.04081487 0.65233231
 [10,] -0.04081487 0.65233231
 [11,] -0.02251459 0.67063259
 [12,] -0.02616434 0.66698285
 [13,] -0.02819600 0.66495118
 [14,] -0.07752280 0.61562439
 [15,] -0.47663409 0.21651309
 [16,] -0.03523314 0.65791404
 [17,] -0.02251459 0.67063259
 [18,] -0.02251459 0.67063259
 [19,] -0.03037798 0.66276920
 [20,] -0.03037798 0.66276920 
... 

和另一个称为Y的数据集,由600个1和0组成。

[1] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 [70] 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[139] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 ... 

如果Y [i] ==“ 1”,我要附加相应的w1_7 [i],如果Y [i] ==“ 0”,我要附加相应的w0_7 [i] 。 因此,预期的输出将例如

>Y 
[1] 0 0 1 
> w
           w0_7       w1_7
  [1,] -0.03272023 0.66042696
  [2,] -0.08887320 0.60427398
  [3,] -0.03037798 0.66276920
> output 
[1] -0.03272023 -0.08887320 0.66276920

有人知道如何用R编写代码吗?

2 个答案:

答案 0 :(得分:1)

我们可以使用矩阵子集从不同的列获取值。

w[cbind(1:nrow(w), y + 1)]
#[1] -0.033 -0.089 -0.030 -0.067 -0.044  0.592 -0.051 -0.026 -0.041

这将在y = 1时从第2列中选择值,在y = 0时从第1列中选择值。

数据

w <- structure(c(-0.03272023, -0.0888732, -0.03037798, -0.06745567, 
-0.04390675, -0.10160427, -0.05075316, -0.02616434, -0.04081487, 
0.66042696, 0.60427398, 0.6627692, 0.62569151, 0.64924043, 0.59154291, 
0.64239402, 0.66698285, 0.65233231), .Dim = c(9L, 2L), .Dimnames = list(
NULL, c("w0_7", "w1_7")))

y <- c(0, 0, 0, 0, 0, 1, 0, 0, 0)

答案 1 :(得分:0)

除了Ronak的答案,您还可以使用data.frame s的方法:

library(dplyr)

# create reproducible example
w = matrix(nrow = 600, ncol = 2)
w[,1] = rnorm(600)
w[,2] = rnorm(600)
y = sample(c(0,1), 600, replace = TRUE)

# data.frame method
wd <- data.frame(w, y)
mutate(wd, out = ifelse(y == 0, X1, X2))

检查结果:

head(wd)

          X1          X2 y        out
1 0.30487377 -1.57575230 1 -1.5757523
2 1.09405936 -0.24520181 1 -0.2452018
3 1.27512478 -0.21452914 1 -0.2145291
4 0.53915030 -0.01437150 0  0.5391503
5 0.09399084  0.56232223 1  0.5623222
6 1.80497849 -0.06664301 0  1.8049785

# and your result is accessible via `$`:
head(wd$out)

[1] -1.5757523 -0.2452018 -0.2145291  0.5391503
[5]  0.5623222  1.8049785