gpt4 book ai didi

r - 不能在嵌套的 data.frame 中子集列

转载 作者:行者123 更新时间:2023-12-05 09:28:14 25 4
gpt4 key购买 nike

我有一个包含多个字符列的 data.frame,还有一个 data.frame。因此,我的 data.frame 中有一个 data.frame。我的目标是将一个字符列与嵌套数据框内的一列子集化。但是,每当我尝试按名称对嵌套列进行子集化时,它都会声明它不存在。您可以在此处查看 data.frame:

df = structure(
list(
`$id` = c("21", "22", "23"),
Id = c("159347",
"161863", "22646"),
Name = c("159347", "161863", "22646"),
SumPeriod = structure(
list(
AccPeriodBasTwrAtMarketPrice = c(0.0969367972082358, 0.537983489472227,-0.107066381156318),
AccPeriodLocTwrAtMarketPrice = c(0.0969367972082358,
0.537983489472227,-0.107066381156318),
BopDate = c(
"2022-02-28T00:00:00",
"2022-02-28T00:00:00",
"2022-02-28T00:00:00"
),
BopBasHoldingValueAtMarketPrice = c(7592266.52,
5135960.59, 7166815.5),
BopBasInterestAccrual = c(0, 0, 0),
EopDate = c(
"2022-02-28T00:00:00",
"2022-02-28T00:00:00",
"2022-02-28T00:00:00"
),
EopBasHoldingValueAtMarketPrice = c(7599626.22,
5163591.21, 7159142.25),
EopBasInterestAccrual = c(0, 0,
0),
AccPeriodBasTwrAtExposureValue = c(0.0969367972082358,
0.537983489472227,-0.107066381156318),
AccPeriodLocTwrAtExposureValue = c(0.0969367972082358,
0.537983489472227,-0.107066381156318),
AccBasIrr = c(0.0969367972082358,
0.537983489472227,-0.107066381156318),
AccLocIrr = c(0.096936797208258,
0.537983489472227,-0.107066381156318),
AccBasMwr = c(0.0484449181280957,
0.268270120259021,-0.0535618639528656),
PeriodBasIrr = c(0.0969367972082358,
0.537983489472227,-0.107066381156318),
PeriodLocIrr = c(0.096936797208258,
0.537983489472227,-0.107066381156318),
PeriodBasTwrAtMarketPrice = c(0.0969367972082358,
0.537983489472227,-0.107066381156318),
PeriodLocTwrAtMarketPrice = c(0.0969367972082358,
0.537983489472227,-0.107066381156318),
PeriodBasTwrDeposit = c(0,
0, 0),
PeriodBasTwrWithdrawal = c(0, 0, 0),
PeriodBasTwrDepositWithdrawal = c(0,
0, 0),
PeriodBasTwrDividendTax = c(0, 0, 0),
PeriodBasTwr = c(7359.70000000112,
27630.6200000001,-7673.25),
PeriodBasMwr = c(0.0484449181280957,
0.268270120259021,-0.0535618639528656),
BenchmarkCalcType = c(
"BenchmarkNotCalculated",
"BenchmarkNotCalculated",
"BenchmarkNotCalculated"
),
EopBenchmarkName = c("",
"", ""),
AccBasBenchmarkReturnPct = c(0, 0, 0),
PeriodBasBenchmarkReturnPct = c(0,
0, 0)
),
class = "data.frame",
row.names = c(NA, 3L)
),
Series = list(
structure(
list(
AccPeriodBasTwrAtMarketPrice = 0.0969367972082358,
AccPeriodLocTwrAtMarketPrice = 0.0969367972082358,
BopDate = "2022-02-28T00:00:00",
BopBasHoldingValueAtMarketPrice = 7592266.52,
BopBasInterestAccrual = 0,
EopDate = "2022-02-28T00:00:00",
EopBasHoldingValueAtMarketPrice = 7599626.22,
EopBasInterestAccrual = 0,
AccPeriodBasTwrAtExposureValue = 0.0969367972082358,
AccPeriodLocTwrAtExposureValue = 0.0969367972082358,
AccBasIrr = 0,
AccLocIrr = 0,
AccBasMwr = 0.0968429207825055,
PeriodBasIrr = 0,
PeriodLocIrr = 0,
PeriodBasTwrAtMarketPrice = 0.0969367972082358,
PeriodLocTwrAtMarketPrice = 0.0969367972082358,
PeriodBasTwrDeposit = 0,
PeriodBasTwrWithdrawal = 0,
PeriodBasTwrDepositWithdrawal = 0,
PeriodBasTwrDividendTax = 0,
PeriodBasTwr = 7359.70000000112,
PeriodBasMwr = 0.0484449181280957,
BenchmarkCalcType = "BenchmarkNotCalculated",
EopBenchmarkName = "",
AccBasBenchmarkReturnPct = 0,
PeriodBasBenchmarkReturnPct = 0
),
class = "data.frame",
row.names = 1L
),
structure(
list(
AccPeriodBasTwrAtMarketPrice = 0.537983489472227,
AccPeriodLocTwrAtMarketPrice = 0.537983489472227,
BopDate = "2022-02-28T00:00:00",
BopBasHoldingValueAtMarketPrice = 5135960.59,
BopBasInterestAccrual = 0,
EopDate = "2022-02-28T00:00:00",
EopBasHoldingValueAtMarketPrice = 5163591.21,
EopBasInterestAccrual = 0,
AccPeriodBasTwrAtExposureValue = 0.537983489472227,
AccPeriodLocTwrAtExposureValue = 0.537983489472227,
AccBasIrr = 0,
AccLocIrr = 0,
AccBasMwr = 0.535104714457055,
PeriodBasIrr = 0,
PeriodLocIrr = 0,
PeriodBasTwrAtMarketPrice = 0.537983489472227,
PeriodLocTwrAtMarketPrice = 0.537983489472227,
PeriodBasTwrDeposit = 0,
PeriodBasTwrWithdrawal = 0,
PeriodBasTwrDepositWithdrawal = 0,
PeriodBasTwrDividendTax = 0,
PeriodBasTwr = 27630.6200000001,
PeriodBasMwr = 0.26827012025902,
BenchmarkCalcType = "BenchmarkNotCalculated",
EopBenchmarkName = "",
AccBasBenchmarkReturnPct = 0,
PeriodBasBenchmarkReturnPct = 0
),
class = "data.frame",
row.names = 1L
),
structure(
list(
AccPeriodBasTwrAtMarketPrice = -0.107066381156318,
AccPeriodLocTwrAtMarketPrice = -0.107066381156318,
BopDate = "2022-02-28T00:00:00",
BopBasHoldingValueAtMarketPrice = 7166815.5,
BopBasInterestAccrual = 0,
EopDate = "2022-02-28T00:00:00",
EopBasHoldingValueAtMarketPrice = 7159142.25,
EopBasInterestAccrual = 0,
AccPeriodBasTwrAtExposureValue = -0.107066381156318,
AccPeriodLocTwrAtExposureValue = -0.107066381156318,
AccBasIrr = 0,
AccLocIrr = 0,
AccBasMwr = -0.107181136120043,
PeriodBasIrr = 0,
PeriodLocIrr = 0,
PeriodBasTwrAtMarketPrice = -0.107066381156318,
PeriodLocTwrAtMarketPrice = -0.107066381156318,
PeriodBasTwrDeposit = 0,
PeriodBasTwrWithdrawal = 0,
PeriodBasTwrDepositWithdrawal = 0,
PeriodBasTwrDividendTax = 0,
PeriodBasTwr = -7673.25,
PeriodBasMwr = -0.0535618639528656,
BenchmarkCalcType = "BenchmarkNotCalculated",
EopBenchmarkName = "",
AccBasBenchmarkReturnPct = 0,
PeriodBasBenchmarkReturnPct = 0
),
class = "data.frame",
row.names = 1L
)
)
),
class = "data.frame",
row.names = c(NA,
3L)
)

所以在上面的 data.frame 中,我唯一感兴趣的列是“Id”和“EopBasHoldingValueAtMarketPrice”,其中后者是嵌套 data.frame 中名为“SumPeriod”的列。我尝试通过:

df_subset = subset(df, select = c("Id", "SumPeriod$EopBasHoldingValueAtMarketPrice"))

但是我得到了错误:

frame`(x, r, vars, drop = drop) : undefined columns selected

你们知道如何实现这一点吗?

最佳答案

SumPeriod 是您的数据框中的一个数据框。所以你在你的标签中建议 dplyr ,你可以用这种方法解决它:

library(dplyr)
library(tidyr)
df %>%
as_tibble() %>%
unnest(SumPeriod) %>%
select(Id, EopBasHoldingValueAtMarketPrice)

输出是:

# A tibble: 3 × 2
Id EopBasHoldingValueAtMarketPrice
<chr> <dbl>
1 159347 7599626.
2 161863 5163591.
3 22646 7159142.

关于r - 不能在嵌套的 data.frame 中子集列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71644358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com