Assign columns to new datasets with a for loop

26 views Asked by At

I tried to do a for loop to assign firms to different funds. I've created a dataset of 2000 different companies. Now I would like to assign the first 50 to fund1, the next 50 to fund2 and so on.

Fund <- list()
for (i in seq(1, 1950, 50)) {
   
   Fund <- subset(artif_company[, i:(i+50)])
    
}

I tried it like that but it doesn't do the right thing xD. Does anybody know how to do that? Or is there another trick probably using tidyquant?

1

There are 1 answers

6
statistikr On

Like that?

companies <- 1:2000
funds <- data.frame(matrix(data = companies, nrow = 50, byrow = F))
names(funds) <- paste0("fund", 1:ncol(funds))