1

I have two datasets. The first one is called Buildings, the data consists of each Building ID with its respective characteristics.

Building_ID      Address         Year       BCR
1                Machida, TY     1994       80
2                Ueno, TY        1972       50
3                Asakusa, TY     1990       70
4                Machida, TY     1982       60
.
.
.
54634            Chiyoda, TY     2002       70

The second dataset is called Residential ID. It only has one table, consisting of the Building ID (which is the same with the Building ID in 'Buildings' dataset) which have Residential usage.

Building_ID
2
3
14
23
39
44
45
133
393
423
.
.

or something like that. What I want to do is to make a new column in my first dataset with regards to my second dataset. I want to categorize which one is a Residential building and which one is not (basically, I want to select all the Buildings ID mentioned in my second dataset and categorize it into Residential in my first dataset). If it is residential, we can name it 'Residential'and else it is 'NR' so it could look something like this:

Building_ID      Address         Year       BCR     Category
1                Machida, TY     1994       80      NR
2                Ueno, TY        1972       50      Residential
3                Asakusa, TY     1990       70      Residential
4                Machida, TY     1982       60      NR
.
.
.
54634            Chiyoda, TY     2002       70      NR

I was thinking it has something to do with ifelse or grepl but so far my code doesn't work.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • If you add the output form `dput(head(DF1))` and `dput(head(DF2))` to your question it'll be easier for answerers to help. Anyway, what you want to do is called a join, you might try searching for this and see if you can't find any helpful answers. in your case, the `%in%` function should actually suffice but learning about joins is a more generalizable skill – MichaelChirico May 10 '18 at 09:00
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1299871 – zx8754 May 10 '18 at 09:14
  • 2
    Try `Buildings$Category <- ifelse(Buildings$Building_ID %in% Residential$Building_ID, "Residential", "NR")` – zx8754 May 10 '18 at 09:15
  • Thank you very much! I am very new in R and I have tried the `%in%` suggested by @MichaelChirico and @zx8754 and it works! – Salsabila Arum May 12 '18 at 06:53

0 Answers0