给定两个相同维度的数据框:
Categories.df1 <- data.frame(Categories)
Returns.df2 <- data.frame(Returns)
Categories.df1
#Fund X31.05.2015 X30.04.2015 X31.03.2015
#1 3 Banken EU Concentrated
#2 3 Banken GL Stock Pickers
#3 3 Banken NAC Stock Pickers
#4 3 Banken O Factor Betters
#5 3V Invest Stock Pickers Stock Pickers Moderately Active
#6 4Q EU Stock Pickers Stock Pickers Stock Pickers
Returns.df2
#Fund X31.05.2015 X30.04.2015 X31.03.2015
#1 3 Banken EU 0.01 0.02
#2 3 Banken GL -0.04 -0.01 0.03
#3 3 Banken NAC -0.02 -0.01 0.04
#4 3 Banken O -0.01 -0.02
#5 3V Invest 0.01 0.02 0.00
#6 4Q EU -0.01 -0.01 0.00
我可以使用Returns.df2中的值替换Categories.df1中的值,条件是“Stock Pickers”(否则为空白)?
因此,我想创建一个新的数据框或子集,如下所示:
Returns.StockPickers.df3
#Fund X31.05.2015 X30.04.2015 X31.03.2015
#1 3 Banken EU
#2 3 Banken GL 0.03
#3 3 Banken NAC 0.04
#4 3 Banken O
#5 3V Invest 0.01 0.02
#6 4Q EU -0.01 -0.01 0.01
任何输入?
答案 0 :(得分:1)
这应该这样做(我更新了片段并包含了一个函数,它接受两个不同的data.frames并返回带有新值的return_data.frame。但是,新值是一个字符串,而不是现在的数字) :
df1 <- data.frame(fund = c("Fund 1", "Fund 2", "Fund 3", "Fund 4"),
X31.05.2015 = c("Stock Picker", "", "Stock Picker", ""))
df2 <- data.frame(fund = c("Fund 1", "Fund 2", "Fund 3", "Fund 4"),
X31.05.2015 = c(1, 2, 3, 4))
fun <- function(cat_df, ret_df, col_name) {
ret_df[, col_name] <- ifelse(cat_df[, col_name] == "Stock Picker", ret_df[, col_name],
"") # change '""' to 0 or NA if you want to get Numerics
return(ret_df)
}
df3 <- fun(df1, df2, col_name = "X31.05.2015")
df3
# fund X31.05.2015
# 1 Fund 1 1
# 2 Fund 2
# 3 Fund 3 3
# 4 Fund 4