gpt4 book ai didi

r - R中的列表操作向量化列表

转载 作者:行者123 更新时间:2023-12-04 05:58:33 26 4
gpt4 key购买 nike

类似于我昨天在 reshaping matrices in R 上的问题,我现在正在尝试 reshape 数据框,以便我可以矢量化我的函数。在下面的代码中,主函数是scorecard .它接收一个名为 subset.loans 的数据框。和 subset.collateral .我想知道我是否可以 reshape 两个框架loanscollaterals ,两者看起来像这样:

  LOANS              COLLATERAL           
id | value id | value type
---------- -------------------
1 200 1 600 a
2 4390 1 899 b
2 860 2 190 d
2 9750 3 4930 e
3 600 3 300 a
: : : : :

进入这个:
id | loans             collateral
-----------------------------
1 c(200) data.frame(a=c(600,899), b=('a','b'))
2 c(4390,860,9750) data.frame(a=c(190), b=c('d'))
3 c(600) data.frame(a=c(4930,300), b=c('e','a'))

我希望如果我这样做,我可以使用 *apply 之一函数 - 或来自 plyr 的东西工具箱 - 简单地应用 scorecard作用于整个事物。如果有更好/更简单的方法,请提及!我目前正在使用的代码(带有一个被遗弃的 for 循环)如下:

# An Nx2 data frame of loans (ID, amount)
loans <- read.table(...)

# An Mx4 data frame of collaterals to loans (ID, type, value, lien)
collateral <- read.table(...)

# One person (ID) can have >1 loan and >1 collateral, so first just
# find all unique IDs
loans.ID.unique = unique(loans$ID)

# Run an analysis on each ID grouping:
for(n in 1:length(loans.ID.unique)) {

# ...all loans for that ID...
subset.loans <- loans$loans[
which(
loans$scorecard_id == loans.ID.unique[n])]

# ...all collateral for that ID...
subset.collateral <- collateral[
which(
collateral$scorecard_id == loans.ID.unique[n]),
c('type','value','lien')]

# Output scores for each ID
scores[n,1] <- loans.ID.unique[n]
scores[n,c(2,3)] <- scorecard(loans=subset.loans,
collateral=subset.collateral,
}

谢谢!

最佳答案

1) 无数据结构 .在 R 中创建这样的结构是不寻常的。建议你随时捕获你需要的东西。这里LoansCollateral是你的两个输入数据框和loanscollateral是当前 id 的部分正在处理。用您自己的代码替换下面函数的双哈希行:

ids <- union(Loans$id, Collateral$id)
do.call("rbind", lapply(ids, function(id) {
loans <- Loans[Loans$id == id, "value"]
collateral <- Collateral[Collateral$id == id, -1]
c(id = id, score = sum(loans) - sum(collateral$value)) ##
}))

添加:

2) 矩阵 .另一方面,如果我们真的想创建这样的结构,可以这样做:
ids <- union(Loans$id, Collateral$id) 
m <- cbind(id = ids,
loans = lapply(ids, function(id) Loans[Loans$id == id, "value"]),
collateral = lapply(ids, function(id) Collateral[Collateral$id == id, -1])
)

do.call("rbind", lapply(1:nrow(m), function(i) with(m[i,],
c(id = id, score = sum(loans) - sum(collateral$value))
)))

3) 数据帧 .我们可以交替将结构表示为数据框 d <- as.date.frame(m)或以下几乎相同:
d <- data.frame(id = ids,
loans = I(lapply(ids, function(id) Loans[Loans$id == id, "value"])),
collateral = I(lapply(ids, function(id) Collateral[Collateral$id == id, -1]))
)
do.call("rbind", lapply(1:nrow(m), function(i) with(d,
c(id = id[[i]], score = sum(loans[[i]]) - sum(collateral[[i]]$value))
)))

编辑:简化了构建 m 的代码.

添加:数据框表示。

关于r - R中的列表操作向量化列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9215279/

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