wlad
wlad

Reputation: 2183

Is it possible to make a list with a wildcard index in R? [with minimal example now]

Is it possible to construct a list in R which has indices 'a','b','c' but if you try to access it by any other index then it gives a specific value rather than an error?

I'm trying to use this to replace a bunch of nested ifelse's and I need to do deal with the final else part.

[edit]

Here's the code I'm trying to replace

All_LDZ$GasRegion<- ifelse(All_LDZ$LDZ=='EA','East Anglia',
                    ifelse(All_LDZ$LDZ=='EM','East Midlands',
                    ifelse(All_LDZ$LDZ=='NE','North East England',
                    ifelse(All_LDZ$LDZ=='NO','North England',
                    ifelse(All_LDZ$LDZ=='NT','North Thames Area',
                    ifelse(All_LDZ$LDZ=='NW','North West England',
                    ifelse(All_LDZ$LDZ=='SC','Scotland',
                    ifelse(All_LDZ$LDZ=='SE','South East England',
                    ifelse(All_LDZ$LDZ=='SO','Southampton Area',
                    ifelse(All_LDZ$LDZ=='SW','South West England',
                    ifelse(All_LDZ$LDZ=='WM','West Midland',
                    ifelse(All_LDZ$LDZ=='WN','North Wales',
                    ifelse(All_LDZ$LDZ=='WS','South Wales', NA)))))))))))))

and I want something like

descriptiveRegionName <- list('EA'="East Anglia",
                            'EM'="East Midlands",
                            'NE'='North East England',
                            'NO'='North England',
                            'NT'='North Thames Area',
                            'NW'='North West England',
                            'SC'='Scotland',
                            'SE'='South East England',
                            'SO'='Southampton Area',
                            'SW'='South West England',
                            'WM'='West Midland',
                            'WN'='North Wales',
                            'WS'='South Wales',
                            ELSE = NA)

Note the ELSE index I made up.

[edit]

This question has been marked as a possible duplicate but I don't see how it's a direct duplicate.

Upvotes: 1

Views: 240

Answers (2)

leerssej
leerssej

Reputation: 14988

Would a join be a more maintainable and efficient approach to this task?

The code for that would look like:

All_LDZ_decoded <- merge(All_LDZ, GasRegionAbbrevTable)

If you named the columns consistently between the tables*, your work is done. If not, then here will help guide the key specifications: https://stackoverflow.com/a/1300618/5088194 or here:https://mkmanu.wordpress.com/2016/04/08/working-with-data-frames-in-r-joins-and-merging/ Though I personally prefer the economy of the dplyr syntax:

Transforming the decoding to a join operation means that we could keep your Abbreviation Table in .csv (from where it might possibly have originated and in which format it would be very easy to modify and manage into the future); we may also continue to use that transportable table in more 'object'-like fashion with other future work, too.

*(and you have normalized your abbrev. table - i.e.: removed duplicates in rows and columns [as in your example])

Upvotes: 0

wlad
wlad

Reputation: 2183

As Ben Bolker suggested

All_LDZ$GasRegion<- match(All_LDZ$LDZ,
                      list('EA'='East Anglia',
                        'EM'='East Midlands',
                        'NE'='North East England',
                        'NO'='North England',
                        'NT'='North Thames Area',
                        'NW'='North West England',
                        'SC'='Scotland',
                        'SE'='South East England',
                        'SO'='Southampton Area',
                        'SW'='South West England',
                        'WM'='West Midland',
                        'WN'='North Wales',
                        'WS'='South Wales'),
                      nomatch = NA)

can replace

All_LDZ$GasRegion<- ifelse(All_LDZ$LDZ=='EA','East Anglia',
                    ifelse(All_LDZ$LDZ=='EM','East Midlands',
                    ifelse(All_LDZ$LDZ=='NE','North East England',
                    ifelse(All_LDZ$LDZ=='NO','North England',
                    ifelse(All_LDZ$LDZ=='NT','North Thames Area',
                    ifelse(All_LDZ$LDZ=='NW','North West England',
                    ifelse(All_LDZ$LDZ=='SC','Scotland',
                    ifelse(All_LDZ$LDZ=='SE','South East England',
                    ifelse(All_LDZ$LDZ=='SO','Southampton Area',
                    ifelse(All_LDZ$LDZ=='SW','South West England',
                    ifelse(All_LDZ$LDZ=='WM','West Midland',
                    ifelse(All_LDZ$LDZ=='WN','North Wales',
                    ifelse(All_LDZ$LDZ=='WS','South Wales', NA)))))))))))))

Upvotes: 1

Related Questions