5

I have what seems a simple requirement, but I can't seem to achieve the desired result. My dataframes are rather large, so I will just give a screenshot:enter image description here

What I want is: if ps2c$ps == present2$pn then ps2c$sf == present2$sf.

If it is not clear, for every ps2c$ps there is a present2$pn (present2 is just the mean data).

5
  • 5
    A screenshot? Are you kidding me? ;-) Seriously, just paste the output from head(ps2c) and head(present2) into your Q, select the pasted code/output, and click the code button in the toolbar (the one that looks like braces {}) or press Ctrl+k to have your code formatted appropriately. Commented Apr 11, 2011 at 9:52
  • 1
    My data frame is massive, but I will bear your suggestion in mind. Commented Apr 11, 2011 at 10:02
  • 1
    but head() only shows the first few lines - my point was, if you can show a screenshot you can show the code and output from head(). Commented Apr 11, 2011 at 10:45
  • 2
    dput(head(ps2c)) gives you a representation of the 6 first lines of the data frame that we can run without editing anything. Alternatively, you can construct a toy dataset to show what you want. Commented Apr 11, 2011 at 10:52
  • @Joris - that is a great observation, one which helps facilitate providing small snippets of data for illustrative purposes. Commented Apr 11, 2011 at 11:08

1 Answer 1

10

You don't supply test data, but something like the following might work:

ps2c$sf <- present2$sf[match(ps2c$ps, present2$pn)]
Sign up to request clarification or add additional context in comments.

1 Comment

I am working with a dataset with 72 million observations, and I was trying to do this task using a not-straightforward routine which involved the foreach function. I tested it for about 1/1000 of my dataset and it took more than 20 minutes. With your solution it took a second for the whole dataset. I can't thank you enough. R really rocks, I can't wait to be good at it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.