Joel B Joel B - 3 months ago 6
R Question

In R can you reference a value within a df$var command

I am processing a variable called buffer_regions from a frame called origframe, which is populated with values like POSTAL_DIST_AB, POSTAL_DIST_AC etc

My dataframe (origframe)

buffer_regions |
-------------- |
POSTAL_DIST_AB |
POSTAL_DIST_AC |
POSTAL_DIST_AD |


I am trying to use the following code to create a new frame (Z_output) with a derived column (called POSTAL_DIST) which is based on origframe$buffer_regions but by removing the string which region is set to ("POSTAL_DIST") and the subsequent underscore. I don't want to hard-code the term POSTAL_DIST anywhere other than when setting the value of region (
region <- "POSTAL_DIST"
) as this is at the top of a larger programme:

require(stringr)
region <- "POSTAL_DIST"
z_output$region <- str_replace_all(final_buffer_list$buffer_regions, region, "")


Desire outcome

buffer_regions | POSTAL_DIST
-------------- | --------------
POSTAL_DIST_AB | AB
POSTAL_DIST_AC | AC
POSTAL_DIST_AD | AD

Answer

We can use substring

origframe$POSTAL_DIST <- substring(origframe$buffer_regions, 
                        nchar(origframe$buffer_regions)-1)
origframe$POSTAL_DIST
#[1] "AB" "AC" "AD"

Or with sub

sub(".*_", "", origframe$buffer_regions)

data

origframe <- structure(list(buffer_regions = c("POSTAL_DIST_AB", 
"POSTAL_DIST_AC", 
"POSTAL_DIST_AD")), .Names = "buffer_regions", class = "data.frame",
row.names = c(NA, -3L))