如果满足特定条件,则递增 R 中的嵌套 for 循环
Incrementing over nested for loop in R if specific condition is met
我是 R 的新手,我正在尝试创建一个嵌套的 for 循环,该循环将递增一系列随机数,并在满足特定条件时将其添加到 table 中的某些变量。下面是示例代码:
#create sample table
tab <- matrix(c(7, 5, 0, 19, 3, 1, 17, 8, 0, 7, 6, 0, 3, 1, 1), ncol=3, byrow=TRUE)
colnames(tab) <- c('V1','V2','V3')
rownames(tab) <- c('abc','def','ghi','jkl', 'mne')
tab <- as.data.frame(tab)
tab
#get random variables
n = 3
rand_num <- round(rnorm(n, mean = 3, sd = 1), digits = 0)
rand_num
#create for loop do add random variables to values where v3 = 0
tab2 <- tab
for(i in 1:nrow(tab))
{
if(tab2$V3[i] == 0)
{
for(p in seq(from = 1, to = 3)
tab2$V2[i] <- tab2$V2[i] + rand_num[p]
}
}
tab
V1 V2 V3
abc 7 5 0
def 19 3 1
ghi 17 8 0
jkl 7 6 0
mne 3 1 1
tab2
V1 V2 V3
abc 7 5 0
def 19 3 1
ghi 17 8 0
jkl 7 6 0
mne 3 1 1
但是,for 循环没有按预期工作。预期结果如下
tab2
V1 V2 V3
abc 7 7 0
def 19 3 1
ghi 17 13 0
jkl 7 8 0
mne 3 1 1
如果有人能指出我正确的方向,我将不胜感激
这是一个没有 for
循环的矢量化解决方案。我已经更改了 n
.
的定义
tab <- matrix(c(7, 5, 0, 19, 3, 1, 17, 8, 0, 7, 6, 0, 3, 1, 1), ncol=3, byrow=TRUE)
colnames(tab) <- c('V1','V2','V3')
rownames(tab) <- c('abc','def','ghi','jkl', 'mne')
tab <- as.data.frame(tab)
tab
#> V1 V2 V3
#> abc 7 5 0
#> def 19 3 1
#> ghi 17 8 0
#> jkl 7 6 0
#> mne 3 1 1
#get random variables
set.seed(2022)
n = sum(tab$V3 == 0)
rand_num <- round(rnorm(n, mean = 3, sd = 1), digits = 0)
rand_num
#> [1] 4 2 2
tab2 <- tab
i <- which(tab2$V3 == 0)
tab2$V2[i] <- tab2$V2[i] + rand_num
由 reprex package (v2.0.1)
于 2022-03-30 创建
这一行循环:
for(p in seq(from = 1, to = 3)
tab2$V2[i] <- tab2$V2[i] + rand_num[p]
正在将所有三个随机数添加到 tab2$V2[i]
的每个值
您可以在这里避免所有循环:
#Define a seed value to make the debugging repeatable
set.seed(1)
#generate random variables
rand_num <- round(rnorm(nrow(tab2), mean = 3, sd = 1), digits = 0)
tab2$V2 <- ifelse(tab2$V3 == 0, tab2$V2+rand_num, tab2$V3)
我是 R 的新手,我正在尝试创建一个嵌套的 for 循环,该循环将递增一系列随机数,并在满足特定条件时将其添加到 table 中的某些变量。下面是示例代码:
#create sample table
tab <- matrix(c(7, 5, 0, 19, 3, 1, 17, 8, 0, 7, 6, 0, 3, 1, 1), ncol=3, byrow=TRUE)
colnames(tab) <- c('V1','V2','V3')
rownames(tab) <- c('abc','def','ghi','jkl', 'mne')
tab <- as.data.frame(tab)
tab
#get random variables
n = 3
rand_num <- round(rnorm(n, mean = 3, sd = 1), digits = 0)
rand_num
#create for loop do add random variables to values where v3 = 0
tab2 <- tab
for(i in 1:nrow(tab))
{
if(tab2$V3[i] == 0)
{
for(p in seq(from = 1, to = 3)
tab2$V2[i] <- tab2$V2[i] + rand_num[p]
}
}
tab
V1 V2 V3
abc 7 5 0
def 19 3 1
ghi 17 8 0
jkl 7 6 0
mne 3 1 1
tab2
V1 V2 V3
abc 7 5 0
def 19 3 1
ghi 17 8 0
jkl 7 6 0
mne 3 1 1
但是,for 循环没有按预期工作。预期结果如下
tab2
V1 V2 V3
abc 7 7 0
def 19 3 1
ghi 17 13 0
jkl 7 8 0
mne 3 1 1
如果有人能指出我正确的方向,我将不胜感激
这是一个没有 for
循环的矢量化解决方案。我已经更改了 n
.
tab <- matrix(c(7, 5, 0, 19, 3, 1, 17, 8, 0, 7, 6, 0, 3, 1, 1), ncol=3, byrow=TRUE)
colnames(tab) <- c('V1','V2','V3')
rownames(tab) <- c('abc','def','ghi','jkl', 'mne')
tab <- as.data.frame(tab)
tab
#> V1 V2 V3
#> abc 7 5 0
#> def 19 3 1
#> ghi 17 8 0
#> jkl 7 6 0
#> mne 3 1 1
#get random variables
set.seed(2022)
n = sum(tab$V3 == 0)
rand_num <- round(rnorm(n, mean = 3, sd = 1), digits = 0)
rand_num
#> [1] 4 2 2
tab2 <- tab
i <- which(tab2$V3 == 0)
tab2$V2[i] <- tab2$V2[i] + rand_num
由 reprex package (v2.0.1)
于 2022-03-30 创建这一行循环:
for(p in seq(from = 1, to = 3)
tab2$V2[i] <- tab2$V2[i] + rand_num[p]
正在将所有三个随机数添加到 tab2$V2[i]
您可以在这里避免所有循环:
#Define a seed value to make the debugging repeatable
set.seed(1)
#generate random variables
rand_num <- round(rnorm(nrow(tab2), mean = 3, sd = 1), digits = 0)
tab2$V2 <- ifelse(tab2$V3 == 0, tab2$V2+rand_num, tab2$V3)