r - Dropping columns in a data frame that are not in another data frame -


i facing problem matching data in 2 data frames. have 1 data frame stock prices , company revenues. however, companies in both data frames not same. here reproducible example of dealing with:

aapl <- c(50,55,75,40,60) msft <- c(80,65,70,75,80) ge <- c(20,25,30,25,35) prices <- data.frame(aapl,msft,ge)  aapl <- c(100,110,120,110,100) msft <- c(200,185,195,170,180) pfe <- c(80,70,80,75,75) revenues <- data.frame(aapl,msft,pfe) 

i have new data frame ratio price/revenues companies in both data frames. this.

aapl.ps <- c(0.5,0.5,0.625,0.364, 0.6) msft.ps <- c(0.4,0.351,0.359,0.441,0.444) price.sales <- data.frame (aapl.ps,msft.ps) 

i new in r , have been struggling while.

any insight on how can this?

thank in advance

you this...

common <- intersect(names(prices),names(revenues)) #common column names  price.sales <- prices[,common]/revenues[,common] #just use columns  price.sales        aapl      msft 1 0.5000000 0.4000000 2 0.5000000 0.3513514 3 0.6250000 0.3589744 4 0.3636364 0.4411765 5 0.6000000 0.4444444 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -