Expanding/Breaking 在 R 中向下一个 for 循环
Expanding/Breaking down a for loop in R
`y <- c()
for( position in c("cannon","jackson","benford","paws","moxie") ) {
n <- nchar(position)
y[position] <- letters[n]
}
y
n
我基本上想做的是编写这个 for 循环幕后的代码,但我 运行 遇到了关于在哪里使用它的问题。这个 for 循环展开后会是什么样子?
position <- "cannon"
n <- nchar(position)
→n <- nchar("cannon")
→n <- 6
y[position] <- letters[n]
→y["cannon"] <- letters[6]
→j["cannon"] <- "f"
position <- "jackson"
n <- nchar(position)
→n <- nchar("jackson")
→n <- 7
y[position] <- letters[n]
→y["jackson"] <- letters[7]
→j["jackson"] <- "g"
等等
`y <- c()
for( position in c("cannon","jackson","benford","paws","moxie") ) {
n <- nchar(position)
y[position] <- letters[n]
}
y
n
我基本上想做的是编写这个 for 循环幕后的代码,但我 运行 遇到了关于在哪里使用它的问题。这个 for 循环展开后会是什么样子?
position <- "cannon"
n <- nchar(position)
→n <- nchar("cannon")
→n <- 6
y[position] <- letters[n]
→y["cannon"] <- letters[6]
→j["cannon"] <- "f"
position <- "jackson"
n <- nchar(position)
→n <- nchar("jackson")
→n <- 7
y[position] <- letters[n]
→y["jackson"] <- letters[7]
→j["jackson"] <- "g"
等等