所以我有一個關鍵因素lk可以生成 1 到 9 之間的亂數。基于該因素,我的資料中必須發生一些變化,如下所示;
我的資料如下所示:
| 雕像 | 成本 | 薪水 |
|---|---|---|
| 噸 | 43 | 21 |
| n | 5 | 17 |
| n | 27 | 21 |
| n | 26 | 35 |
| 噸 | 39 | 38 |
| n | 12 | 22 |
| 噸 | 42 | 13 |
| 噸 | 38 | 32 |
| n | 39 | 15 |
| 噸 | 25 | 38 |
如果lk > sum (df$statue=="t")說lk == 7 等sum (df$statue=="t") == 5兩個statue=="n" 最小的 人cost 必須將他們的狀態從 這個示例中的個人 2 和 6更改n 為 。t
如果lk <= sum (df$statue=="t")說lk == 3 等sum (df$statue=="t") == 5兩個statue=="t" 最大的 人salary 必須將他們的狀態從 這個示例中的個人 5 和 10更改t 為 。n
我希望我說清楚了;
請有任何想法;
此致
uj5u.com熱心網友回復:
不知道這是不是你想要的...
check_lk <- function(lk, data){
sum_t <- nrow(data[data$statue == "t",])
if(lk > sum_t){
# create reordered data, by statue and cost (ascending order)
tmp <- data[order(data$statue, data$cost),]
# first two records represent statue n and two lowest cost
# add another conditional check if needed
tmp[1:2, "statue"] <- "t"
tmp <- tmp[ order(as.numeric(row.names(tmp))), ]
} else{
# create reordered data, by statue and salary (ascending order)
tmp <- data[order(data$statue, data$salary),]
tmp[c(nrow(tmp), nrow(tmp)-1), "statue"] <- "n"
tmp <- tmp[ order(as.numeric(row.names(tmp))), ]
}
return(tmp)
}
加載資料給定:
df <- structure(list(statue = c("t", "n", "n", "n", "t", "n", "t",
"t", "n", "t"), cost = c(43L, 5L, 27L, 26L, 39L, 12L, 42L, 38L,
39L, 25L), salary = c(21L, 17L, 21L, 35L, 38L, 22L, 13L, 32L,
15L, 38L)), class = "data.frame", row.names = c(NA, -10L))
測驗 lk = 7
check_lk(7, df)
statue cost salary
1 t 43 21
2 t 5 17
3 n 27 21
4 n 26 35
5 t 39 38
6 t 12 22
7 t 42 13
8 t 38 32
9 n 39 15
10 t 25 38
測驗 lk=3
check_lk(3, df)
statue cost salary
1 t 43 21
2 n 5 17
3 n 27 21
4 n 26 35
5 n 39 38
6 n 12 22
7 t 42 13
8 t 38 32
9 n 39 15
10 n 25 38
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515919.html
標籤:r数据库变量变量
上一篇:顫振將方法應用于靜態變數
