扩展/破坏R中的for循环

时间:2018-09-21 15:12:08

标签: r for-loop position rstudio nchar

`y <- c()

for( position in c("cannon","jackson","benford","paws","moxie") ) {
n <- nchar(position)
y[position] <- letters[n]
}

y

n

我本质上想做的是编写此for循环幕后的代码,但是我遇到了在哪里使用它的问题。这样的for循环扩展会是什么样子?

1 个答案:

答案 0 :(得分:0)

  1. λ_A = 4.0 λ_B = 5.0 a = np.array([0,0,0,1,0]) b = np.array([0,0,1,1,0]) d = {'A':a, 'B':b} df = pd.DataFrame(d) df = df.assign(poiss_A = poisson.pmf(df['A'], λ_A * (df['A'].index + 1) / len(df))) df = df.assign(poiss_b = poisson.pmf(df['B'], λ_B * (df['B'].index + 1) / len(df))) df Out[125]: A B poiss_A poiss_b 0 0 0 0.449329 0.367879 1 0 0 0.201897 0.135335 2 0 1 0.090718 0.149361 3 1 1 0.130439 0.073263 4 0 0 0.018316 0.006738 Time_Interval = pd.timedelta_range('11:00:00', periods=5, freq='10T') df.index = Time_Interval df Out[128]: A B poiss_A poiss_b 11:00:00 0 0 0.449329 0.367879 11:10:00 0 0 0.201897 0.135335 11:20:00 0 1 0.090718 0.149361 11:30:00 1 1 0.130439 0.073263 11:40:00 0 0 0.018316 0.006738

    • position <- "cannon"n <- nchar(position)n <- nchar("cannon")
    • n <- 6y[position] <- letters[n]y["cannon"] <- letters[6]
  2. j["cannon"] <- "f"

    • position <- "jackson"n <- nchar(position)n <- nchar("jackson")
    • n <- 7y[position] <- letters[n]y["jackson"] <- letters[7]