随着时间的时间更新值

发布于 2025-02-13 07:22:57 字数 3143 浏览 0 评论 0原文

因此,我有一个复杂的问题,我必须模拟随时间变化(25年) 我的数据范围如下:

年龄edustatuexp_ysalcostsal_acost_a cost_a
26fnoem27610000 0
20mlowunem00 00287150
24mlowunem200345200
24mnoem103801500 00
36fhieghem1868430000
42flowem1764624500

变更条件为:

if(statu ==“ unem”&然后sal_a> cost_a)然后更新这些变量的值{statu =“ em”,age = age+1,exp_y = exp_y+1,sal = sal_a*(1+0.19*exp_y),cost_a,cost_a,sal_a = 0和sal_a = 0和0和cost_a = 0}

if(statu ==“ em”& sal< cost)so {statu =“ unem”;年龄=年龄+1,sal_a = sal,cost_a = cost,exp_y = exp_y,sal = 0和成本= 0}

任何想法请求,

善意问候

So i have a complexe problem where I have to simulate the change of statu over time (25 years)
my dataframe presented like this:

agesexedustatuexp_ysalcostsal_acost_a
26fnoem27610000
20mlowunem000287150
24mlowunem200345200
24mnoem1038015000
36fhieghem1868430000
42flowem1764624500

change conditions are :

if (statu=="unem" & sal_a>cost_a) then update the values of these variables {statu = "em",age=age+1, exp_y=exp_y+1, sal=sal_a*(1+0.19*exp_y), cost=cost_a, sal_a=0 and cost_a=0}

if (statu=="em" & sal<cost) so {statu = "unem" ; age=age+1, sal_a=sal, cost_a=cost, exp_y=exp_y, sal=0 and cost=0}

Any ideas pleas,

kind regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你再特殊 2025-02-20 07:22:57

第一个条件,请尝试此情况

for(x in 1:nrow(df)) 
    if(df$statu[x] == "unem" & df$sal_a[x] > df$cost_a[x]) 
    {
    df$statu[x] <-  "em"
    df$age[x] <-  df$age[x] + 1
    df$exp_y[x] <-  df$exp_y[x] +1
    df$sal[x] <- df$sal_a[x]*(1+0.19*df$exp_y[x])
    df$cost[x] <- df$cost_a[x]
    df$sal_a[x] <- 0
    df$cost_a[x] <- 0
    }

对于第二个条件

for(x in 1:nrow(df)) 
    if(df$statu[x] == "em" & df$sal[x] < df$cost[x]) 
    {
    df$statu[x] <-  "unem"
    df$age[x] <-  df$age[x] + 1
    df$sal_a[x] <- df$sal[x]
    df$cost_a[x] <- df$cost[x]
    df$exp_y[x] <-  df$exp_y[x]
    df$sal[x] <- 0
    df$cost[x] <- 0
    }
  • 数据
df <- structure(list(age = c(27, 21, 25, 24, 36, 42), sex = c("f", 
"m", "m", "m", "f", "f"), edu = c("no", "low", "low", "no", "hiegh", 
"low"), statu = c("unem", "em", "em", "em", "em", "em"), exp_y = c(2, 
1, 3, 10, 18, 17), sal = c(0, 341.53, 541.65, 380, 684, 646), 
    cost = c(0, 150, 200, 150, 300, 245), sal_a = c(76, 0, 0, 
    0, 0, 0), cost_a = c(100, 0, 0, 0, 0, 0)), row.names = c(NA, 
-6L), class = "data.frame")
  • 输出的
  age sex   edu statu exp_y    sal cost sal_a cost_a
1  27   f    no  unem     2   0.00    0    76    100
2  21   m   low    em     1 341.53  150     0      0
3  25   m   low    em     3 541.65  200     0      0
4  24   m    no    em    10 380.00  150     0      0
5  36   f hiegh    em    18 684.00  300     0      0
6  42   f   low    em    17 646.00  245     0      0

Try this for the first condition

for(x in 1:nrow(df)) 
    if(df$statu[x] == "unem" & df$sal_a[x] > df$cost_a[x]) 
    {
    df$statu[x] <-  "em"
    df$age[x] <-  df$age[x] + 1
    df$exp_y[x] <-  df$exp_y[x] +1
    df$sal[x] <- df$sal_a[x]*(1+0.19*df$exp_y[x])
    df$cost[x] <- df$cost_a[x]
    df$sal_a[x] <- 0
    df$cost_a[x] <- 0
    }

for the second condition

for(x in 1:nrow(df)) 
    if(df$statu[x] == "em" & df$sal[x] < df$cost[x]) 
    {
    df$statu[x] <-  "unem"
    df$age[x] <-  df$age[x] + 1
    df$sal_a[x] <- df$sal[x]
    df$cost_a[x] <- df$cost[x]
    df$exp_y[x] <-  df$exp_y[x]
    df$sal[x] <- 0
    df$cost[x] <- 0
    }
  • Data
df <- structure(list(age = c(27, 21, 25, 24, 36, 42), sex = c("f", 
"m", "m", "m", "f", "f"), edu = c("no", "low", "low", "no", "hiegh", 
"low"), statu = c("unem", "em", "em", "em", "em", "em"), exp_y = c(2, 
1, 3, 10, 18, 17), sal = c(0, 341.53, 541.65, 380, 684, 646), 
    cost = c(0, 150, 200, 150, 300, 245), sal_a = c(76, 0, 0, 
    0, 0, 0), cost_a = c(100, 0, 0, 0, 0, 0)), row.names = c(NA, 
-6L), class = "data.frame")
  • Output
  age sex   edu statu exp_y    sal cost sal_a cost_a
1  27   f    no  unem     2   0.00    0    76    100
2  21   m   low    em     1 341.53  150     0      0
3  25   m   low    em     3 541.65  200     0      0
4  24   m    no    em    10 380.00  150     0      0
5  36   f hiegh    em    18 684.00  300     0      0
6  42   f   low    em    17 646.00  245     0      0
此岸叶落 2025-02-20 07:22:57

您可以尝试一下:

# Number of years
  y <- 25

# Create a list
  evol <- list()
  # Make "y" copies of your df
    for (i in 1:y){evol[[i]] <- df}

# At each step (strting in the second element) evaluate the changes
  for (i in 2:y){
  # Logical vector (including the positions of rows to change)
    v.logic  <- evol[[i-1]]$statu=="unem" & evol[[i-1]]$sal_a> evol[[i]]$cost_a
    v.logic2 <- evol[[i-1]]$statu=="em" & evol[[i-1]]$sal<evol[[i-1]]$cost
  # Values to change  
    # Fist condition
      evol[[i]][v.logic,"age"] <- evol[[i-1]][v.logic,"age"] + 1
      evol[[i]][v.logic,"exp_y"] <- evol[[i-1]][v.logic,"exp_y"] + 1
      evol[[i]][v.logic,"sal"] <- evol[[i-1]][v.logic,"sal_a"]*(1+0.19*evol[[i-1]][v.logic,"exp_y"])
      evol[[i]][v.logic, "cost"] <- evol[[i-1]][v.logic,"cost_a"]
      evol[[i]][v.logic, "sal_a"] <- 0
      evol[[i]][v.logic, "cost_a"] <- 0
    # Second condition
      evol[[i]][v.logic2,"statu"] <- "unem"
      evol[[i]][v.logic2, "age"] <- evol[[i-1]][v.logic2, "age"] + 1
      evol[[i]][v.logic2, "sal_a"] <- evol[[i-1]][v.logic2, "sal"]
      evol[[i]][v.logic2, "cost_a"] <- evol[[i-1]][v.logic2, "cost"]
      evol[[i]][v.logic2, "sal"] <- 0
      evol[[i]][v.logic2, "cost"] <- 0
    }

这将导致第i-the元素代表I-the年度表的列表。

You can try this:

# Number of years
  y <- 25

# Create a list
  evol <- list()
  # Make "y" copies of your df
    for (i in 1:y){evol[[i]] <- df}

# At each step (strting in the second element) evaluate the changes
  for (i in 2:y){
  # Logical vector (including the positions of rows to change)
    v.logic  <- evol[[i-1]]$statu=="unem" & evol[[i-1]]$sal_a> evol[[i]]$cost_a
    v.logic2 <- evol[[i-1]]$statu=="em" & evol[[i-1]]$sal<evol[[i-1]]$cost
  # Values to change  
    # Fist condition
      evol[[i]][v.logic,"age"] <- evol[[i-1]][v.logic,"age"] + 1
      evol[[i]][v.logic,"exp_y"] <- evol[[i-1]][v.logic,"exp_y"] + 1
      evol[[i]][v.logic,"sal"] <- evol[[i-1]][v.logic,"sal_a"]*(1+0.19*evol[[i-1]][v.logic,"exp_y"])
      evol[[i]][v.logic, "cost"] <- evol[[i-1]][v.logic,"cost_a"]
      evol[[i]][v.logic, "sal_a"] <- 0
      evol[[i]][v.logic, "cost_a"] <- 0
    # Second condition
      evol[[i]][v.logic2,"statu"] <- "unem"
      evol[[i]][v.logic2, "age"] <- evol[[i-1]][v.logic2, "age"] + 1
      evol[[i]][v.logic2, "sal_a"] <- evol[[i-1]][v.logic2, "sal"]
      evol[[i]][v.logic2, "cost_a"] <- evol[[i-1]][v.logic2, "cost"]
      evol[[i]][v.logic2, "sal"] <- 0
      evol[[i]][v.logic2, "cost"] <- 0
    }

It will result on a list where the i-th element represents the table for the i-th year.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文