0

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?

user438383
  • 5,716
  • 8
  • 28
  • 43
Rafael
  • 1
  • 1
  • what's your expected output? could you also add a reproducible example? – B. Christian Kamgang Nov 04 '22 at 09:41
  • I try to assign the first 50 "assets" to fund1, the next 50 to fund2 and so on. For each of the funds I get (2000/50 = 40 funds) I would like to calculate the mean return. – Rafael Nov 04 '22 at 09:50
  • ```set.seed(19914159) n <- 3 artif_company <- tibble(Company = rnorm(n, mean = 0.07, sd = 0.2)) for (i in 1:1999) { i <- tibble(Company = rnorm(n, mean = 0.07, sd = 0.2)) artif_company <- cbind2(artif_company, i) } Fund <- list() for (i in seq(1, 1950, 50)) { Fund <- subset(artif_company[, i:(i+50)]) }``` – Rafael Nov 04 '22 at 10:00
  • Greetings! It would be more helpful to add your code chunk in the comments into the question itself. Listing it in the comments makes it harder to read/work with. – Shawn Hemelstrand Nov 16 '22 at 07:00

1 Answers1

0

Like that?

companies <- 1:2000
funds <- data.frame(matrix(data = companies, nrow = 50, byrow = F))
names(funds) <- paste0("fund", 1:ncol(funds))
  • Well I do have the dataset of the artificial companies with the returns. But I don't really know how to assign them columnwise to the funds. I need 40 funds consisting 50 companies each – Rafael Nov 04 '22 at 10:09
  • why do you want n=3 random draws, i.e. multiple observations per firm? The result would then not fit in a two-dimensional matrix... how should your output look like? 50row,40cols? Then you would need to draw only 1 per company, or multiple and then aggregate to 1 – statistikr Nov 04 '22 at 10:10
  • That should be the random returns of each firm for 3 years. ```funds <- data.frame(matrix(data = artif_company, nrow = 50, byrow = F)) names(funds) <- paste0("fund", 1:ncol(funds))``` When I do that, I get 40 funds with 50 firms each, but the entries are vectors. Not sure if I can access e.g., the first element of each vector(firm) of fund 1. – Rafael Nov 04 '22 at 10:14
  • My goal is to compute the mean return of each fund in year 1 and year 2. Exactly, I tried to get 50rows and 40 cols Well I could also compute two dataframes, one with year 1 return and the other with year 2 returns? In order to not get vectors as entries. Isn't that possible? – Rafael Nov 04 '22 at 10:16
  • n.years = 2 return <- replicate(2000, mean(rnorm(n.years, mean = 0.07, sd = 0.2))) funds <- data.frame(matrix(data = return, nrow = 50, byrow = F)) names(funds) <- paste0("fund", 1:ncol(funds)) apply(funds, 2, mean) – statistikr Nov 04 '22 at 10:29
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 09 '22 at 10:24