-3

I want to repeat a column vector that have 300rows about 241times and to concatonate it. The data is downloadable in this link. https://1drv.ms/u/s!AiZLoqatH-p7rD0og-RufSi6fljB

I tried the following code.

read.csv("stack_overflow.csv")
fund_name = d[,1] 
fund_name_panel=c() 
    for (i in 1:300{x1=rep(fund_name[i], 241)  fund_name_Panel=append(x1,fund_name_panel)}

Result: unfortunately, My code repeats only the very last row of the data. How can i repeat each of the 300rows rather than the very last? Any hint is appreciated.

user438383
  • 5,716
  • 8
  • 28
  • 43
Enrico Dace
  • 115
  • 10
  • 2
    Hi Enrico, please take the time to read the guidance on asking questions, as it will help make it easier for others to assist. Having a reproducible example is important though as it lets other replicate your error: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Michael Harper Mar 03 '18 at 18:48
  • I also cannot understand exactly what the question is asking. Does `result <- c(); for(i in 1:300){ result <- append(i, result)}` capture the basic beahviour? – Michael Harper Mar 03 '18 at 18:54
  • @MikeyHarper I think, now it's even better explained. I modified it. I appreciate the feedback. – Enrico Dace Mar 03 '18 at 22:26

1 Answers1

0

From your description of the problem you are committing a very simple error a lot of people make when first learning for loops. First since you are making a new variable (fund_name_panel) you need to create an empty vector the length of the vector you will use in the for loop.

fund_name_panel <- numeric(length(fund_name))

Use nrow() instead of length() if fund_name is a data.frame and not a vector.

Secondly, you will need to specify the row (i) in both the now new vector (fund_name_panel) and the vector of you are referencing in the for loop (fund_name) see code below.

fund_name_panel <- numeric(length(fund_name))

for(i in 1:length(fund_name)){
x[i]=y[i]
}
Enrico Dace
  • 115
  • 10
THATguy
  • 292
  • 2
  • 11