Solved

Comparing values between more than 2 datasets in R

Posted on 2014-04-25
2
202 Views
Last Modified: 2014-05-01
Hello,

I am relatively new to R. I am amazed by how much stuff I can get done with it. Right now I am doing a project. I have a three data sets that contain the returns of the entire stocks in the S&P 500, Ranks of the book to market value of these stocks and ranks of the market capitalization of these stocks. I have divided my book to market ranks into 'top', 'bottom','middle' where 'top' represents the top 30%, 'bottom' represents the low 30% and 'middle' represents the middle 40%. The ranks of the market capitalization are divided into 'small' and 'big'.

I have to create 3 portfolios that have an intersection of 'small' and 'bottom' and then find those stocks in the return matrix. 'small' and 'top' and find the stocks in the return matrix. 'small' and 'middle' and find the stocks in the return matrix. I have to go row by row since the values of these stocks keep changing every month. So for example if one stock is 'top' one month it might be 'middle' the next month same goes for my 'small' and 'big' matrix. I have tried using the %in% function but is there a better way that I can do this without losing myself into messy code. Your help is greatly appreciated.

Thank you,
Abhishek
0
Comment
Question by:anajbile
2 Comments
 
LVL 1

Accepted Solution

by:
Chelsea Obrochta earned 500 total points
Comment Utility
Hi!

You can use the merge function. For example lets say your file is named stock. Your variable are rank2(for big and small) and rank3(for top, middle, and bottom). You then do:

        merge(stock[rank2== "small",], stock[rank3== "top",])

This would give you a list of all stocks in small and top.

I hope this Helps! Let me know :)
Chelsea
0
 

Author Comment

by:anajbile
Comment Utility
Hi Chelsea, Thank for the solution. I found that intersect function works as well.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now