Maya
Maya

Reputation: 43

append elements to vector in R returns only latest value

I would like to read through a for loop some data in a dataframe and create a ratio using them. The ratio is well calculated as the singular output is fine. However, I wish to create a vector out of the ratios. The issue is that the output of the vector is always the latest ratio. Could anybody help on that, please? Thanks.

for(i in seq(from = 0, to = nrow(newdata2018), by = 2)){
  ratio1vector <- NULL

  ratio1 <- newdata2018$A_01[i]/newdata2018$A[i]*100
  ratio1vector <- c(ratio1vector, ratio1)

  print(ratio1vector)
}

Upvotes: 0

Views: 36

Answers (1)

Maya
Maya

Reputation: 43

To solve the issue is sufficient to create the vector externally from the loop.

Upvotes: 1

Related Questions