-1

I tried to write a loop that would, say, return me a vector of ten numeric entries. It went well, until i attempted to label each element of the output generated by the block with another line. The naming is supposed to produce each entry with their unique label characters: "number"[n] where n is number in the sequence of repeats which was used in the generation of numeric vector.

multiples <- c()
i <- c(1:10)
for (n in i) {
    print (n * 10)
    multiples[n] <- n * 10
    names(multiples)[n] <- "number"[n]
}
multiples

The execution is successful but the result is not what i'm expecting. Is there a way to name the elements correctly using the same for loop?

dd_rookie
  • 331
  • 2
  • 3
  • 13

1 Answers1

0

I dont think you need loop here.

Are you trying to do this?

i <- 1:10
multiples <- i * 10
names(multiples) <- paste0("number", i)

multiples
#number1  number2  number3  number4  number5  number6  number7  number8  
#  10       20       30       40       50       60       70       80 
#number9 number10 
#  90      100 

If you want to stick to for loop, you can

i <- c(1:10)
multiples <- numeric(10)
names(multiples) <- ""
for (n in i) {
multiples[n] <- n * 10
names(multiples)[n] <- paste0("number", n)
}
Roland
  • 127,288
  • 10
  • 191
  • 288
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks. Exactly that. But how is the same end achieved using the "for" loop? As a newbie to statistical programming in R, i would prefer to stick to the understanding of most basic explicit loops. – dd_rookie Sep 16 '16 at 07:01
  • @DeependraDhakal updated the answer. Hope it is helpful. – Ronak Shah Sep 16 '16 at 07:17
  • 1
    Please don't teach bad practices. You know how big the vector is going to be. Pre-allocate it. – Roland Sep 16 '16 at 07:25
  • 1
    @DeependraDhakal You'll never learn proper R if you do that. The design of the language is such, that `for` loops are often an inferior solution.Learn to think in vectors. – Roland Sep 16 '16 at 07:26
  • @DeependraDhakal That's really a very bad idea to insist on a `for` loop here. If you want to use for loops for such cases, then I suggests some of the traditional 40-50 year old languages instead of R. As Roland pointed out, you won't learn anything useful this way. – RHertel Sep 16 '16 at 07:32
  • @Roland Thanks for the edit. I agree `for` loop is definitely an inferior solution in R. However, for a new user I believe some time needs to be given for them to settle in as specially if they are coming from C or C++ background. Using vectorized solution from day one might end up scaring them. – Ronak Shah Sep 16 '16 at 07:32
  • @RonakShah A new user must learn the language, and not transfer concepts of old programming languages to new ones. What you suggest concerning the frequent use of for loops may be correct for old C-style programs, but modern C++ hardly contains any `for` loop - much like R doesn't. – RHertel Sep 16 '16 at 07:34
  • @Roland if you mean inferior in terms of execution time then i'm convinced (heard from programmers). But I've also frequently found in programming courses that the basic types of explicit loop are initially discussed, progressively they move to advances ones. That's how you improve isn't it? – dd_rookie Sep 16 '16 at 07:35
  • @RHertel Maybe , you are correct. However, I prefer learning from doing rather than directly accepting what masters in that language say. I must understand why I shouldn't use `for` loop in R. Telling a user this is the correct way and you need to follow this or else you'll learn nothing is quite intimidating. – Ronak Shah Sep 16 '16 at 07:37
  • @DeependraDhakal If you come from a stats (or maths) background (and that's the target user group) then thinking in vectors is very natural. I actually teach `for` loops pretty late (after `apply`, `lapply`). – Roland Sep 16 '16 at 07:48
  • If you want to understand why `for` loops are inferior: Learn to benchmark and just compare the amount of code between your two alternatives. – Roland Sep 16 '16 at 07:49
  • @Roland Can you refer me to a simple instance of benchmarking? – dd_rookie Sep 16 '16 at 08:03
  • http://adv-r.had.co.nz/Performance.html You should also study http://www.burns-stat.com/pages/Tutor/R_inferno.pdf – Roland Sep 16 '16 at 10:46