0
zz <- data.frame()
appended_list <- data.frame()
a=0
for(a in 10) {
  zz=data.frame(filter(new_DataSet, RP_ENTITY_ID == assets[a]))
  appended_list <- list(appended_list, zz)
}

Dear all,

I would need a list of dataframes but this code just appends columns of "new_DataSet" like so: enter image description here

I would like append list or dataframes object in the main list (appended_list)

Thanks

  • 1
    Welcome to SO! Please see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). You can provide your data via the output of `dput(new_DataSet)` or to give a minimal subset use `dput(head(new_DataSet))`. – AndrewGB Feb 01 '22 at 00:17
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 06 '22 at 13:29

1 Answers1

1

I'm not entirely sure what your expected output should be (or what assets is). If you are just needing to create subsets of your dataframe and put them into a list, then we can use lapply or purrr.

lapply(c(1:10), function(x) {
  df[df$RP_ENTITY_ID == x,]
})

Or using purrr:

library(tidverse)

map(c(1:10), function(x) {
  df %>% filter(RP_ENTITY_ID == x)
})

Output

[[1]]
  RP_ENTITY_ID   b  c
1            1 200 51

[[2]]
  RP_ENTITY_ID   b  c
1            2 199 52

[[3]]
  RP_ENTITY_ID   b  c
1            3 198 53

[[4]]
  RP_ENTITY_ID   b  c
1            4 197 54

[[5]]
  RP_ENTITY_ID   b  c
1            5 196 55

[[6]]
  RP_ENTITY_ID   b  c
1            6 195 56

[[7]]
  RP_ENTITY_ID   b  c
1            7 194 57

[[8]]
  RP_ENTITY_ID   b  c
1            8 193 58

[[9]]
  RP_ENTITY_ID   b  c
1            9 192 59

[[10]]
  RP_ENTITY_ID   b  c
1           10 191 60

Data

df <-
  structure(
    list(
      RP_ENTITY_ID = 1:100,
      b = 200:101,
      c = 51:150
    ),
    class = "data.frame",
    row.names = c(NA,-100L)
  )
AndrewGB
  • 16,126
  • 5
  • 18
  • 49