根据R中的列值保留某些列值/将其归零

mqkwyuun  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(130)

我正在处理可能包含所有可能AY的 Dataframe (2000 ≤ AY ≤ 2005,i = P0、P1、P2、P3、P4、P5),如图(a)所示。

  1. AY P0 P1 P2 P3 P4 P5
  2. 2000 250 0 0 0 0 45
  3. 2001 0 50 0 0 70 30
  4. 2002 20 0 0 0 0 60
  5. 2003 500 0 0 10 0 0
  6. 2004 10 45 90 0 5 0
  7. 2005 70 0 0 55 0 30

或者可能不喜欢这个(b)

  1. AY P0 P1 P2 P3 P4 P5
  2. 2001 750 50 0 0 70 30
  3. 2002 20 30 0 0 0 60
  4. 2003 60 0 50 10 0 0
  5. 2005 50 65 0 55 0 30

同样,i = P0,P1,P2,P3,P4,P5,令P0 = 0,P1 = 1,P2 = 2,P3 = 3,P4 = 4,P5 = 5,我只想保留column/s i满足/y AY + i ≤ 2005的列值,并将其余值等于0。
期望输出
(一)

  1. AY P0 P1 P2 P3 P4 P5
  2. 2000 250 0 0 0 0 45
  3. 2001 0 50 0 0 70 0
  4. 2002 20 0 0 0 0 0
  5. 2003 500 0 0 0 0 0
  6. 2004 10 45 0 0 0 0
  7. 2005 70 0 0 0 0 0

(乙)

  1. AY P0 P1 P2 P3 P4 P5
  2. 2001 750 50 0 0 70 0
  3. 2002 20 30 0 0 0 0
  4. 2003 60 0 50 0 0 0
  5. 2005 50 0 0 0 0 0

这里,如果AY = 2005,则仅P0的列值保持原样。如果AY = 2003,则仅P0:P2的列值保持原样。依此类推。
我该怎么做呢?

gojuced7

gojuced71#

  1. tab1[-1] <- Map(function(P, AY, val) ifelse((P+AY) <= 2005, val, 0),
  2. as.integer(sub("\\D", "", names(tab1)[-1])),
  3. list(tab1$AY), tab1[,-1])
  4. tab2[-1] <- Map(function(P, AY, val) ifelse((P+AY) <= 2005, val, 0),
  5. as.integer(sub("\\D", "", names(tab2)[-1])),
  6. list(tab2$AY), tab2[,-1])
  7. tab1
  8. # AY P0 P1 P2 P3 P4 P5
  9. # 1 2000 250 0 0 0 0 45
  10. # 2 2001 0 50 0 0 70 0
  11. # 3 2002 20 0 0 0 0 0
  12. # 4 2003 500 0 0 0 0 0
  13. # 5 2004 10 45 0 0 0 0
  14. # 6 2005 70 0 0 0 0 0
  15. tab2
  16. # AY P0 P1 P2 P3 P4 P5
  17. # 1 2001 750 50 0 0 70 0
  18. # 2 2002 20 30 0 0 0 0
  19. # 3 2003 60 0 50 0 0 0
  20. # 4 2005 50 0 0 0 0 0

数据类型

  1. tab1 <- structure(list(AY = 2000:2005, P0 = c(250L, 0L, 20L, 500L, 10L, 70L), P1 = c(0L, 50L, 0L, 0L, 45L, 0L), P2 = c(0L, 0L, 0L, 0L, 90L, 0L), P3 = c(0L, 0L, 0L, 10L, 0L, 55L), P4 = c(0L, 70L, 0L, 0L, 5L, 0L), P5 = c(45L, 30L, 60L, 0L, 0L, 30L)), class = "data.frame", row.names = c(NA, -6L))
  2. tab2 <- structure(list(AY = c(2001L, 2002L, 2003L, 2005L), P0 = c(750L, 20L, 60L, 50L), P1 = c(50L, 30L, 0L, 65L), P2 = c(0L, 0L, 50L, 0L), P3 = c(0L, 0L, 10L, 55L), P4 = c(70L, 0L, 0L, 0L), P5 = c(30L, 60L, 0L, 30L)), class = "data.frame", row.names = c(NA, -4L))
展开查看全部

相关问题