Using a substring on a column in R
How can I use a substring to only use the first 3 digits of the postal code in the data sheet?
YEAR PERSON POSTALCODE STORE_ID
2012 245345 M2H 2I4 20001319
2012 234324 L6N 3R5 20001319
2012 556464 L6N 4T5 20001319
This is a piece of code I tried, however, my datasheet came up with 0 objects after I added the subscript (I assume I made a very dumb mistake):
combined <- merge(df1, df2, by.y="PERSON")
store1 <- combined[combined$STORE_ID == 20001319 && substr(combined$POSTALCODE, 1, 3), ]
+3
source to share
1 answer
substr(combined$POSTALCODE, 1, 3)
gives you
# [1] "M2H" "L6N" "L6N"
So one possible choice would be
combined[combined$STORE_ID == 20001319 & substr(combined$POSTALCODE, 1, 3) == "M2H", ]
which gives you a subset of
# YEAR PERSON POSTALCODE STORE_ID
# 1 2012 245345 M2H 2I4 20001319
+6
source to share