0

I want to sum up all rows from many dataframes. My dataframes are named SP1, SP2, ... , SP48

I tried it with:

for (i in 1:48) {
 nrow(paste0("SP", i)) 
}

But as it seems, paste0 gives back a charachter, so nrow cant calculate anything.

Thank you

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
MA_1760
  • 31
  • 5
  • [Put them in a list](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames). – Gregor Thomas Nov 09 '22 at 15:50
  • 1
    `df_list = mget(ls(pattern = "SP[0-9]+"))` then `sum(sapply(df_list, nrow))` – Gregor Thomas Nov 09 '22 at 15:51
  • worked perfectly could u explain how the pattern coding worked? – MA_1760 Nov 09 '22 at 15:53
  • It's a regex pattern `"SP"` matches literally "SP", and then `[0-9]+` matches 1 or more digits. So it looks for all objects in your R environment that are named "SP" followed by numbers. Since you know your names and they are simple, you could have `paste`d them together directly, `df_list = mget(paste0("SP", 1:48))`. Using a regex pattern gives more flexibility (which isn't really necessary here). – Gregor Thomas Nov 09 '22 at 16:00

0 Answers0