Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:13:31+00:00 2026-06-13T00:13:31+00:00

I am new to R and am puzzled by this problem when manipulating some

  • 0

I am new to R and am puzzled by this problem when manipulating some environmental monitoring data.

I have two datasets recording the actual monitoring time-series and the monitoring site information, respectively. I stored them in two data frames monitoring and sites:

monitoring:

                  date       site obs
 1 2001-01-01 10:00:00  riverside  NA
 2 2001-01-01 11:00:00  riverside  52
 3 2001-01-01 12:00:00  riverside  52
 4 2001-01-01 13:00:00  riverside  56
 5 2001-01-01 10:00:00       dorm  52
 6 2001-01-01 11:00:00       dorm  64
 7 2001-01-01 12:00:00       dorm  76
 8 2001-01-01 13:00:00       dorm  80
 9 2001-01-01 10:00:00        kfc  78
10 2001-01-01 11:00:00        kfc  74
11 2001-01-01 12:00:00        kfc  66
12 2001-01-01 13:00:00        kfc  68

sites:

        site       type
 1      DORM   suburban
 2       KFC      urban
 3 RIVERSIDE      rural

I want to add a site.type column in monitoring with information extracted from sites as shown below:

                  date       site obs site.type
 1 2001-01-01 10:00:00  riverside  NA     rural
 2 2001-01-01 11:00:00  riverside  52     rural
 3 2001-01-01 12:00:00  riverside  52     rural
 4 2001-01-01 13:00:00  riverside  56     rural
 5 2001-01-01 10:00:00       dorm  52  suburban
 6 2001-01-01 11:00:00       dorm  64  suburban
 7 2001-01-01 12:00:00       dorm  76  suburban
 8 2001-01-01 13:00:00       dorm  80  suburban
 9 2001-01-01 10:00:00        kfc  78     urban
10 2001-01-01 11:00:00        kfc  74     urban
11 2001-01-01 12:00:00        kfc  66     urban
12 2001-01-01 13:00:00        kfc  68     urban

I tried grep() in the following command:

for (i in 1:nrow(monitoring)) {
  monitoring$site.type[i] <- as.character(sites$type[grep(monitoring$site[i], sites$site, ignore.case = T)])
}

It worked OK on this small example set of monitoring. However, when I applied it to my real dataset with 654,525 records, it never stopped running on my i5-2400 computer with 16 GB RAM…

I tried to search for existing questions on stackoverflow and did find some answers offering the same solution to similar scenarios, so was even more confused why it did not work in my case. Therefore,

  1. Could someone kindly point out where the problem is?
  2. May I ask how to avoid for looping in this case, as it may
    not be as “fashionable” and efficient? 🙂

Many thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:13:32+00:00Added an answer on June 13, 2026 at 12:13 am

    Proper way to do it is to use merge, as Ben suggested, but here is a simple trick:

    rownames( sites ) <- tolower( sites$site )
    

    Now you can get access sites using keys such as riverside, for example try sites[ "riverside", ]. The tolower() function is used only to turn RIVERSIDE into riverside. Therefore, you can do

    monitoring$site.type <- sites[ monitoring$site, "type" ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to PHP and MySQL, have heard amazing things about this website from Leo
This problem is really puzzling to me: I have the following script working on
I am somewhat new to Perl, and this bug has puzzled me for a
I have this weird problem that's killing us. I have a widely used app
Really puzzled on this, probably simple but it is the day after New Years.
I'm puzzled over the following code: Proc.new do |a| a.something test puts a.something puts
New to Regex. I want to validate to this format: Any character allowed, except
New to Node.js and Express, I am trying to understand the two seems overlapping
New programmer here, I am trying to understand and break down this code below
Originally, I was having some issues getting this code to function, but after a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.