• Status: Solved
• Priority: Medium
• Security: Public
• Views: 257

# Comparing values between more than 2 datasets in R

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
anajbile
1 Solution

Accounting InternCommented:
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 Commented:
Hi Chelsea, Thank for the solution. I found that intersect function works as well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.