将行值替换为同一行中特定于行的列索引值的左侧

时间:2016-12-13 07:27:18

标签: r indexing dataframe replace

数据表按生命年数显示项目的状态报告:

def <- data.frame(c("ProjA", "ProjB", "ProjC"), c("0", "2", "2"), 
              c("Active", "Cancelled", "Distressed"), c("Active", NA, "Distressed"), 
              c("Active", "Cancelled", "Distressed"), c("Active", NA, "Distressed"), stringsAsFactors = FALSE)
colnames(def) <- c("proj.name", "status.update.year", "year.0", "year.1", "year.2", "year.3")
def$status.update.year <- as.numeric(def$status.update.year)

def

  proj.name status.update.year     year.0     year.1     year.2     year.3
1     ProjA                  0     Active     Active     Active     Active
2     ProjB                  2  Cancelled       <NA>  Cancelled       <NA>
3     ProjC                  2 Distressed Distressed Distressed Distressed

status.update.year变量记录从"Active"到另一个状态的更改年份。

项目应该从"Active"开始,但是在过去的几年中,在第3年成为"Distressed"的项目现在被报告为"Distressed"。这种错误的回溯是我想纠正的。

我想将标记为“0”的列的行值更改为“3”,这样在状态更新年之前,所有非NA观察值在状态更改年份之前都标记为“活动”,结果像这样:

  proj.name status.update.year     year.0     year.1     year.2     year.3
1     ProjA                  0     Active     Active     Active     Active
2     ProjB                  2     Active       <NA>  Cancelled       <NA>
3     ProjC                  2     Active     Active Distressed Distressed

我可以在 最后一次非NA观察之前找到每行 的所有非NA观测值:

apply(def[ ,3:6], 1, function(x) { head(x[!is.na(x)], -1) }) 

如果这些值在每个特定行"Active"之前,请使用status.update.year替换这些值?

2 个答案:

答案 0 :(得分:1)

试试这个:

for co in `git branch -a --no-merge`; do  git log -1 --no-walk --pretty=format:"%h - %an, %ar : %s" $co; done | grep <knownUserName>

答案 1 :(得分:0)

以下是来自set

data.table的另一个选项
library(data.table)
setDT(def)
for(j in 0:3) {
  j1 <- grep(j, names(def))
  set(def, i = which(!is.na(def[[j1]]) & j < def$status.update.year),
                                   j = j1, value = "Active")
 }