使用以下示例数据
Model <- c(1,1,1,1,1,2,2,2,2,4,4,4,4,3,3,3,3,3)
FactorID <- c ("Factor1", "Factor2", "Factor4", "Factor3", "Factor5", "Factor2", "Factor3", "Factor4", "Factor1", "Factor2", "Factor3", "Factor4", "Factor1", "Factor3", "Factor2", "Factor4", "Factor5", "Factor1")
FactorName<- c("SEK", "GBP", "USD", "CAD", "YEN", "GBP", "USD", "CAD", "EUR", "CAD", "EUR", "USD", "GBP", "YEN", "CAD", "EUR", "USD", "SEK")
a <- data.frame(Model,FactorID,FactorName)
Model <- c(2,1,3,4)
Factor1 <- c(0.054, 0.113, 0.903, 0.720)
Factor2 <- c(0.885, 0.153, 0.708, 0.750)
Factor3 <- c(0.430, 0.989, 0.518, 0.843)
Factor4 <- c(0.533, 0.6328, 0.343, 0.961)
Factor5 <- c("-", 0.055, 0.699, "-")
b <- data.frame(Model,Factor1,Factor2,Factor3,Factor4,Factor5)
我希望将数据帧b拆分为4个数据帧(每个模型一个),并在数据帧a中找到适当的列标题名称(例如,EUR,GBP等,而不是Factor1,Factor2等)。
答案 0 :(得分:1)
你可以这样做。它将两个dfs按Model
拆分,然后遍历它们,将a
的列与b
的列名匹配,并将相应的货币指定为新名称。
bSplit <- split(b,b$Model)
aSplit <- split(a,a$Model)
for(i in seq_along(bSplit)){
names(bSplit[[i]])[-1] <-
as.character(aSplit[[i]]$FactorName)[
match(names(bSplit[[i]])[-1],
aSplit[[i]]$FactorID)]
}
bSplit
$`1`
Model SEK GBP CAD USD YEN
2 1 0.113 0.153 0.989 0.6328 0.055
$`2`
Model EUR GBP USD CAD NA
1 2 0.054 0.885 0.43 0.533 -
$`3`
Model SEK CAD YEN EUR USD
3 3 0.903 0.708 0.518 0.343 0.699
$`4`
Model GBP CAD EUR USD NA
4 4 0.72 0.75 0.843 0.961 -
答案 1 :(得分:1)
这是另一种方式。我首先通过将FactorName
和a
转换为字符来重新排列FactorID
FactorName
的顺序。然后,我将b
分割为Model
。在lapply()
中,我为每个FactorName
获取Model
并使用它们来重写列名称。
library(dplyr)
arrange(a, Model, FactorID) %>%
mutate_all(funs(as.character(.))) ->a
split(b, f = b$Model) -> whatever
lapply(1:length(ana), function(x){
foo <- a$FactorName[a$Model == x]
names(whatever[[x]]) <- c("Model", foo)
whatever[[x]]
})
#[[1]]
# Model SEK GBP CAD USD YEN
#2 1 0.113 0.153 0.989 0.6328 0.055
#
#[[2]]
# Model EUR GBP USD CAD NA
#1 2 0.054 0.885 0.43 0.533 -
#
#[[3]]
# Model SEK CAD YEN EUR USD
#3 3 0.903 0.708 0.518 0.343 0.699
#
#[[4]]
# Model GBP CAD EUR USD NA
#4 4 0.72 0.75 0.843 0.961 -
答案 2 :(得分:0)
早上好,
我已经为您提出了一个解决方案,其中包括:
query.getResultList().stream().findFirst().orElse(null);