我想在每個缺少它們的串列中添加一個0
and 。1000000
dat <- structure(list(cat = structure(c(1L, 3L, 2L), levels = c("A",
"B", "C"), class = "factor"),
strata = list(c(25, 100, 250, 500), c(25,
100, 250, 500), c(25, 100, 1000000
))), row.names = c(NA, -3L), class = c("data.table", "data.frame"
))
cat strata
1: A 25,100,250,500
2: C 25,100,250,500
3: B 25, 100,1000000
我嘗試執行以下操作:
dat$strata <- lapply(dat$strata, \(x){
if (0 %nin% dat$strata) {
dat$strata<- append(0, dat$strata)
}
if (1000000 %nin% dat$strata) {
dat$strata<- append(dat$strata, 1000000)
}
})
但我做錯了什么。
期望的輸出:
cat strata
1: A 0, 25,100,250,500,1000000
2: C 0, 25,100,250,500,1000000
3: B 0, 25, 100,1000000
這里的正確語法是什么?
uj5u.com熱心網友回復:
一個簡單的方法是在任何地方添加 0 和 1000000 并獲取唯一值
dat$strata[] <- lapply(dat$strata, function(i)unique(c(0, i, 1000000)))
dat
cat strata
1 A 0, 25, 100, 250, 500, 1000000
2 C 0, 25, 100, 250, 500, 1000000
3 B 0, 25, 100, 1000000
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495783.html