0

I tried using the advice given in Put multiple data frames into list (smart way) but continue getting the following error:

argument "nm" is missing, with no default

I'm not sure why this is the case. I tried searching the error but couldn't find anything on it (Create a list with named values by applying a function to each row of a data frame shows this error in a comment but didn't explain it).

I have the following data.frames (data doesn't matter only name here):

#create data.frames
p1sfdf1=data.frame(a=(1:10),b=(2:11),c=(3:12))
p2sfdf1=data.frame(a=(1:10),b=(2:11),c=(3:12))
p1sfdf2=data.frame(a=(1:10),b=(2:11),c=(3:12))
p2sfdf2=data.frame(a=(1:10),b=(2:11),c=(3:12))
p1sfdf3=data.frame(a=(1:10),b=(2:11),c=(3:12))
p2sfdf3=data.frame(a=(1:10),b=(2:11),c=(3:12))
p1sfdf4=data.frame(a=(1:10),b=(2:11),c=(3:12))
#note no p2sfdf4 here
p1sfdf5=data.frame(a=(1:10),b=(2:11),c=(3:12))
p2sfdf5=data.frame(a=(1:10),b=(2:11),c=(3:12))

#combine data.frames into a list of data.frames
l.sf=setNames(lapply(ls(pattern="p[1-2]+"),function(x) get(x)))
                     

I would like to group the data.frames into a list of data.frames. I tried to group based off of the pattern "p" and then either 1 or 2 but I wasn't sure how to demonstrate that in the pattern. I'm guessing that's where my error comes from, but I'm not sure how to fix it.

johnnyg
  • 129
  • 8
  • You can use `mget` to create a list: `mget(ls(pattern = "^p\\dsfdf\\d$"))` Not sure what you mean by "group the data.frames" - can you post the expected output? – markus Jul 30 '20 at 21:25
  • It looks like a list for objects starting with `p1` is wanted and the another with objects `p2` to finally join all lists in one list. – Duck Jul 30 '20 at 21:48

2 Answers2

2

You aren't passing any names to setNames:

l.sf <- lapply(ls(pattern="p[1-2]+") ,function(x) get(x))
l.sf <- setNames(l.sf, ls(pattern="p[1-2]+"))

str(l.sf)
#> List of 9
#>  $ p1sfdf1:'data.frame': 10 obs. of  3 variables:
#>   ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..$ b: int [1:10] 2 3 4 5 6 7 8 9 10 11
#>   ..$ c: int [1:10] 3 4 5 6 7 8 9 10 11 12
#>  $ p1sfdf2:'data.frame': 10 obs. of  3 variables:
#>   ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..$ b: int [1:10] 2 3 4 5 6 7 8 9 10 11
#>   ..$ c: int [1:10] 3 4 5 6 7 8 9 10 11 12
#>  $ p1sfdf3:'data.frame': 10 obs. of  3 variables:
#>   ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..$ b: int [1:10] 2 3 4 5 6 7 8 9 10 11
#>   ..$ c: int [1:10] 3 4 5 6 7 8 9 10 11 12
#>  $ p1sfdf4:'data.frame': 10 obs. of  3 variables:
#>   ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..$ b: int [1:10] 2 3 4 5 6 7 8 9 10 11
#>   ..$ c: int [1:10] 3 4 5 6 7 8 9 10 11 12
#>  $ p1sfdf5:'data.frame': 10 obs. of  3 variables:
#>   ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..$ b: int [1:10] 2 3 4 5 6 7 8 9 10 11
#>   ..$ c: int [1:10] 3 4 5 6 7 8 9 10 11 12
#>  $ p2sfdf1:'data.frame': 10 obs. of  3 variables:
#>   ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..$ b: int [1:10] 2 3 4 5 6 7 8 9 10 11
#>   ..$ c: int [1:10] 3 4 5 6 7 8 9 10 11 12
#>  $ p2sfdf2:'data.frame': 10 obs. of  3 variables:
#>   ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..$ b: int [1:10] 2 3 4 5 6 7 8 9 10 11
#>   ..$ c: int [1:10] 3 4 5 6 7 8 9 10 11 12
#>  $ p2sfdf3:'data.frame': 10 obs. of  3 variables:
#>   ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..$ b: int [1:10] 2 3 4 5 6 7 8 9 10 11
#>   ..$ c: int [1:10] 3 4 5 6 7 8 9 10 11 12
#>  $ p2sfdf5:'data.frame': 10 obs. of  3 variables:
#>   ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..$ b: int [1:10] 2 3 4 5 6 7 8 9 10 11
#>   ..$ c: int [1:10] 3 4 5 6 7 8 9 10 11 12

Created on 2020-07-30 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1

We can simply get a named list with mget

lst1 <- mget(ls(pattern = "^p[1-2]+")
akrun
  • 874,273
  • 37
  • 540
  • 662