简体   繁体   English

R:使用另一个数据框的映射在一个数据框中创建一个新列

[英]R: Create a new column in a data frame using a mapping from another data frame

I have the following data frames. 我有以下数据框。

> temp
  x1    x2
1  1 INDIA
2  2 INDIA
3  3    US
4  4    US

> PortfolioIndices
  Country   Index CCY
1   INDIA   CNX50 INR
2      US   SP500 USD
3      UK FTSE100 GBP

I want to add one more column to temp with currencies corresponding to the respective countries in x2 column using the mapping from PortfolioIndices data frame. 我想使用来自PortfolioIndices数据框的映射在temp中再添加一列,并使用与x2列中的相应国家/地区对应的货币。 Something like this should be the output 像这样的东西应该是输出

> temp
  x1    x2  x3
1  1 INDIA INR
2  2 INDIA INR
3  3    US USD
4  4    US USD

I don't want to use for loop as the actual data could be very large and using a for loop in that case would be very inefficient. 我不想使用for循环,因为实际数据可能非常大,在这种情况下使用for循环效率很低。 Is there a better way to achieve the given output? 有没有更好的方法来实现给定的输出?

Thanks in advance. 提前致谢。

You can use merge : 您可以使用merge

> merge(temp, PortfolioIndices, by.x = "x2", by.y = "Country")
     x2 x1 Index CCY
1 INDIA  1 CNX50 INR
2 INDIA  2 CNX50 INR
3    US  3 SP500 USD
4    US  4 SP500 USD

Here is a data.table approach, recomended for big tables. 这是data.table方法,推荐用于大表。

require(data.table)

# I imagine you have your data in data.frames.

temp <- data.frame(x1 = c(1:4), x2 = c("INDIA", "INDIA", "US", "US"))

PortfolioIndices <- data.frame(Country = c("INDIA", "US", "UK"),
                            Index = c("CNX50", "SP500", "FTSE100"),
                            CCY = c("INR", "USD", "GBP"))                           

# Coerce your data to data.table objects (they are still data.frames) and use the J() 
# function    

temp <- as.data.table(temp)
PortfolioIndices <- as.data.table(PortfolioIndices)
setkey(temp, x2)
setkey(PortfolioIndices, Country)

PortfolioIndices[temp, list(x1,Index,CCY),]

#   Country x1 Index CCY
# 1:   INDIA  1 CNX50 INR
# 2:   INDIA  2 CNX50 INR
# 3:      US  3 SP500 USD
# 4:      US  4 SP500 USD

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:如何用另一个数据框中的“ countif”值在数据框中创建新列? - R: How to create a new column in a data frame with “countif” values from another data frame? 根据 R 中的列名创建一个包含来自另一个数据框中的列的新数据框 - create a new data frame with columns from another data frame based on column names in R 使用 R 中另一个数据框中的值在数据框中创建新变量 - Create New Variable in an data frame using values from another data frame in R 从另一个数据框中查询数据以获取r中的新列 - Query data from another data frame for new column in r 从一个数据框的不同列创建一个新列,该条件以另一个数据框的另一列为条件 - Create a new column from different columns of one data frame conditioned on another column from another data frame 在R中创建一个包含另一个数据帧统计信息的新数据框 - Create a new data frame in R containing statistics of another data frame 使用一个data.frame中的数据为R中另一个data.frame中的新列生成值 - Using data in one data.frame to generate values for a new column in another data.frame in R 如何使用在r中重复的另一个数据框中的特定列更新数据框中的新列? - How to update new column in data-frame with specific column from another data-frame with duplicated in r? 使用for循环在数据框中创建新列以计算R中的值? - Create new column in data frame using a for loop to calculate value in R? 尝试使用 R 中的 function 在数据框中创建新列 - Trying to create a new column in a data frame using a function in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM