简体   繁体   中英

Select data from one dataframe by comparing against another dataframe in R

Basically I have two datasets, one is on the firm level and one is on the industry level.

The values below are contained the both datasets, although may not be in the same order. The row numbers are different as well.

date <- c(1999,2000,2001...2019)
industry <- c("communication services", "utilities", "financials")

dffirm <- c(firmID, date, industry, beta, asset_ratios)
dfindustry <- c(industry, date, mean_beta, mean_asset_ratios)

What I want to do is to compare beta and asset ratios between the two datasets with corresponding industry and year so I can create a portfolio of firms based on certain criteria, such as:

dffirm$beta < dfind$beta & dffirm$asset_ratio > dfindustry$mean_asset_ratio

Based on your comments, I think you are looking for basic summary statistics for each of the data frames to compare. I think using tidyverse packages could be helpful.

EDITED

install.packages("dplyr")
install.packages("magrittr")
library("magrittr")
library("dplyr")


#some sample datasets
firm_data <- data.frame("date" = c(rep(2009,4),rep(2010,4),rep(2011,4)),
                 "firm_beta" = rnorm(12))


industry_data <- data.frame("date" = c(rep(2009,4),rep(2010,4),rep(2011,4)),
                 "industry" = rep(c("industry1","indsutry2"),6),
                 "industry_beta" = rnorm(12))


#caculate mean for column "beta" in data frame 1
industry_data2 <- industry_data %>% group_by(date) %>% 
     summarise(industry_mean_beta = mean(industry_beta))
industry_data2


#caculate mean for column "beta" in data frame 2
firm_data2 <- firm_data %>% group_by(date) %>% summarise(firm_mean_beta = mean(firm_beta))
firm_data2



dc3 <- industry_data2 %>% left_join(firm_data2) %>% 
            mutate(beta_differ = case_when(
                  industry_mean_beta > firm_mean_beta ~ "industry > firm",
                  industry_mean_beta < firm_mean_beta ~ "industry > firm",
                  industry_mean_beta == firm_mean_beta ~ "industry > firm"
            ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM