0

I have this function

GWR.function <- function(shape1,shape2,shape3,x,y,...)

there are 3 shapefiles, I want R to allow shape2 and shape3 to be missing.

Although for example if I use a if(missing(shape2)) {} and then input:

GWR.function(NY.council.data,Borough.Areas,'PERCENT.WHITE.NON.HISPANIC',
     'PERCENT.NRECEIVES.PUBLIC.ASSISTANCE','PERCENT.FEMALE','PERCENT.MALE')

R will not recognise that there is only 2 shapefiles and that the second one is missing.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Pass the shapes parameters as a single list of shapes and then process the list dependent on its length. – SteveM Dec 26 '20 at 00:44

1 Answers1

1

You can pass parameters by name; if the names don't match shape2 or shape3, those won't be passed. For example,

 GWR.function(shape1 = NY.council.data, x = Borough.Areas, 
              y = 'PERCENT.WHITE.NON.HISPANIC', 
              a = 'PERCENT.NRECEIVES.PUBLIC.ASSISTANCE',
              b = 'PERCENT.FEMALE',
              c = 'PERCENT.MALE')

Alternatively, if you want to specify them by position, just don't put anything in those positions, e.g.

GWR.function(NY.council.data, , , # The two previous params are missing
             Borough.Areas, 'PERCENT.WHITE.NON.HISPANIC', 
             'PERCENT.NRECEIVES.PUBLIC.ASSISTANCE', 'PERCENT.FEMALE', 'PERCENT.MALE')  
user2554330
  • 37,248
  • 4
  • 43
  • 90