[英]Comparing two data frames based on a column in R
I'm trying to find a way to program the following in R 我正在尝试找到一种在R中编写以下内容的方法
I have a data frame in which I will be sorting the table based on the lowest growth rates for a set of companies 我有一个数据框,其中我将根据一组公司的最低增长率对表进行排序
Table 1 表格1
Company Growth-Dept1
A 5%
B 10%
C 15%
D 20%
Table 2 表2
Company Growth-Dept2
A 2%
M 4%
D 8%
C 2%
I want to compare Table2 based on the Company names in Table1 and generate a Table3 that only has matching names 我想根据表1中的公司名称比较表2并生成仅具有匹配名称的表3
Output Table 3 输出表3
Company Growth-Dept2
A 2%
C 2%
D 8%
Will I need to use a loop in this case? 在这种情况下是否需要使用循环? Any other way to program this?
还有其他编程方法吗? I have around 800 entries in each column
每栏中大约有800个条目
How does this look? 看起来如何?
t1 <- data.frame(company=c('A','B','C','D'),growth=c(.05,.1,.15,.2))
t2 <- data.frame(company=c('A','M','D','C'),growth=c(.02,.04,.08,.02))
tcombined <- t2[which(t1$company %in% t2$company),]
tcombined <- tcombined[order(tcombined$growth),]
tcombined
gives 给
company growth
1 A 0.02
4 C 0.02
3 D 0.08
Using data.table
along with scales
for percentages. 使用
data.table
以及scales
百分比。
library(data.table)
library(scales)
dt1 <- data.table(Company=c('A','B','C','D'),growth_dep1=percent(c(.05,.1,.15,.2)), key = "Company")
dt2 <- data.table(Company=c('A','M','D','C'),growth_dep2=percent(c(.02,.04,.08,.02)), key = "Company")
dt1[dt2][!is.na(growth_dep1), .(Company, growth_dep2)][order(growth_dep2)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.