数据集的复杂子集(Complex subsetting of dataframe)

编程入门 行业动态 更新时间:2024-10-25 13:25:38
数据集的复杂子集(Complex subsetting of dataframe)

考虑以下数据帧:

df <- data.frame(Asset = c("A", "B", "C"), Historical = c(0.05,0.04,0.03), Forecast = c(0.04,0.02,NA)) # Asset Historical Forecast #1 A 0.05 0.04 #2 B 0.04 0.02 #3 C 0.03 NA

以及变量x 。 x由用户在R脚本的开头设置,可以取两个值: x = "Forecast"或x = "Historical" 。

如果x = "Forecast" ,我想返回以下内容:对于每个资产,如果预测可用,则从“预测”列返回相应的数字,否则,从“历史”列返回相应的数字。 如下所示,A和B都有一个预测值,该值在下面返回。 C缺少预测值,因此返回历史值。

Asset Return 1 A 0.04 2 B 0.02 3 C 0.03

但是,如果x= "Historical" ,则只返回Historical列:

Asset Historical 1 A 0.05 2 B 0.04 3 C 0.03

我无法想出一个简单的方法,如果你有大量的行,暴力是非常低效的。 有任何想法吗?

谢谢!

Consider the following dataframe:

df <- data.frame(Asset = c("A", "B", "C"), Historical = c(0.05,0.04,0.03), Forecast = c(0.04,0.02,NA)) # Asset Historical Forecast #1 A 0.05 0.04 #2 B 0.04 0.02 #3 C 0.03 NA

as well as the variable x. x is set by the user at the beginning of the R script, and can take two values: either x = "Forecast" or x = "Historical".

If x = "Forecast", I would like to return the following: for each asset, if a forecast is available, return the appropriate number from the column "Forecast", otherwise, return the appropriate number from the column "Historical". As you can see below, both A and B have a forecast value which is returned below. C is missing a forecast value, so the historical value is returned.

Asset Return 1 A 0.04 2 B 0.02 3 C 0.03

If, however, x= "Historical",simply return the Historical column:

Asset Historical 1 A 0.05 2 B 0.04 3 C 0.03

I can't come up with an easy way of doing it, and brute force is very inefficient if you have a large number of rows. Any ideas?

Thanks!

最满意答案

首先,预处理您的数据:

df2 <- transform(df, Forecast = ifelse(!is.na(Forecast), Forecast, Historical))

然后提取两列选择:

df2[c("Asset", x)]

First, pre-process your data:

df2 <- transform(df, Forecast = ifelse(!is.na(Forecast), Forecast, Historical))

Then extract the two columns of choice:

df2[c("Asset", x)]

更多推荐

本文发布于:2023-08-04 16:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子集   数据   Complex   subsetting   dataframe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!