替代循环ifelse()的矢量数据 [英] Alternative to loop ifelse() for vector data
问题描述
我有一个价格数据框 dat
,第二个数据框的货币为 cur
。我想要做的是将 dat
中的每个非欧元价格转换为欧元,并使用作为()
函数,嵌套 ifelse()
。 else
价格已经是EUR,函数不应该做任何事情,并将原始值返回到 Price
in dat
。
dat:
国家价格价格€
AT 10
AT 12
BE 15
BG 30
BG 40
CZ 200
cur:
国家利率
BG 0.51
CZ 0.03918
只有非欧盟国家包含在 cur
中。
我用这个代码:
for(i in 1:length(cur)){
if (dat $ Nation == cur $ Nation [i]){
dat $ Price * cur $ Rate [i]
}
else {}
}
输出应该是这样的:
dat:
国家价格价格€
在10 NA
在12 NA
BE 15 NA
BG 30 15.3
BG 40 20.4
CZ 200 7.836
是在价格€
栏(那些是货币为€的国家)中填写新来港定居人士 - 由于没有告知该功能如何处理其他
- 循环结束后
index< - is.na(dat $ Price€)
/ pre>
dat $ Price€[index]< - dat $ Price [index]
dat:
国家价格价格€
AT 10 10
AT 12 12
BE 15 15
BG 30 15.3
BG 40 20.4
CZ 200 7.836
这里的问题是,R给出了一个错误信息:
pre $警告信息
1:在if(dat $ nation == cur $ Nation [i]){:
中,条件长度> 1,并且只有第一个元素将被使用
2:In if(dat $ Nation == cur $ Nation [i]){:
,条件长度> 1,只有第一个元素将被使用
它所做的是将所有价格值与第一次交换率(0.51),然后停止在两个数据框中为列 Nation
查找相同的值以应用不同的汇率。这只是我的数据集中的一个简单例子。欧盟内部每个国家都有多个条目(n = 1740)。
这可能听起来是一个复杂的方法,但是我对R很陌生,知道如何解决这个功能或者是一个替代功能。
干杯
比较一个向量与中的一个字符if(dat $ Nation == cur $ Nation [i])
,所以错误告诉你它只会比较< code $ dat $ Nation 与 cur $ Nation [i]
。
在R中,for循环通常是不必要的(而且很慢)。你可以做以下几个例子:
$ $ $ $ $ $ c $ dat $ $ conv $ cur $ rate [match(dat $ Nation,cur $国家)]#添加转换系数
dat $ PriceE< - ifelse(is.na(dat $ conv),dat $ Price,dat $ Price * dat $ conv)
dat $ conv< - NULL
输出:
国家价格价格
1 AT 10 10.000
2 AT 12 12.000
3 BE 15 15.000
4 BG 30 15.300
5 BG 40 20.400
6 CZ 200 7.836
希望这有助于您!
I have a price dataframe dat
and a second dataframe with currencies cur
. What i want to do is to transform every non-EUR price in dat
into Euro with a for()
function and nested ifelse()
. else
a price already is EUR, the function should do nothing and return the original value into the column Price €
in dat
.
dat:
Nation Price Price€
AT 10
AT 12
BE 15
BG 30
BG 40
CZ 200
cur:
Nation Rate
BG 0.51
CZ 0.03918
Only countries with non-EU currency are contained in cur
.
I used this code:
for (i in 1:length(cur)){
if(dat$Nation == cur$Nation[i]){
dat$Price * cur$Rate[i]
}
else { }
}
The output should be something like this:
dat:
Nation Price Price€
AT 10 NA
AT 12 NA
BE 15 NA
BG 30 15.3
BG 40 20.4
CZ 200 7.836
The idea is to fill NAs in the Price€
column (those are countries with € currency) - resulting from not telling the function what to do in the case of else
- with values from Price
after the loop finished.
index <- is.na(dat$Price€)
dat$Price€[index] <- dat$Price[index]
dat:
Nation Price Price€
AT 10 10
AT 12 12
BE 15 15
BG 30 15.3
BG 40 20.4
CZ 200 7.836
My problem here is, that R gives an error message:
Warning messages:
1: In if (dat$Nation == cur$Nation[i]) { :
the condition has length > 1 and only the first element will be used
2: In if (dat$Nation == cur$Nation[i]) { :
the condition has length > 1 and only the first element will be used
What it does is multiplying all price values with the first exchange rate (0.51) and then stopping to look for identical values for the columns Nation
in both dataframes to apply a different exchange rate. This is just a simple example from my dataset. There are multiple entries for each country within the EU (n=1740).
This might sound a complicated way to do this, but I am new to R and would like to know how to fix this function or what an alternative function would be.
Cheers
The problem with your code is that you are comparing a vector against a character in if (dat$Nation == cur$Nation[i])
, so the error tells you that it will only compare the first element of dat$Nation
with cur$Nation[i]
.
In R, for-loops are often not necessary (and slow). You could do for example the following:
dat$conv <- cur$Rate[match(dat$Nation,cur$Nation)] # add the conversion factor
dat$PriceE <- ifelse(is.na(dat$conv), dat$Price, dat$Price * dat$conv)
dat$conv <- NULL
Output:
Nation Price PriceE
1 AT 10 10.000
2 AT 12 12.000
3 BE 15 15.000
4 BG 30 15.300
5 BG 40 20.400
6 CZ 200 7.836
Hope this helps!
这篇关于替代循环ifelse()的矢量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!