R 查找从列底部开始的第一个非零元素的索引
在76x108维的数据帧dt中,我希望通过最小化列中的非零元素(从最后一行开始),将每列13到108中的值之和减少存储在数组c中的量 例如,如果dt[76,13]>0,则发生以下情况:R 查找从列底部开始的第一个非零元素的索引,r,dataframe,R,Dataframe,在76x108维的数据帧dt中,我希望通过最小化列中的非零元素(从最后一行开始),将每列13到108中的值之和减少存储在数组c中的量 例如,如果dt[76,13]>0,则发生以下情况: dt[76,13]这将实现您的目的。(我用cc重命名了向量c,这样它就不会与函数c交互) df% 突变(跨越(以('TB')开头,~-c(首先是pmin(cc[as.integer(str_remove(cur_column(),'TB')))))), cumsum(pmin(,cc[as.integer(str
dt[76,13]这将实现您的目的。(我用cc
重命名了向量c
,这样它就不会与函数c
交互)
df%
突变(跨越(以('TB')开头,~-c(首先是pmin(cc[as.integer(str_remove(cur_column(),'TB')))))),
cumsum(pmin(,cc[as.integer(str_remove(cur_column(),'TB'))))),
diff(pmin(cc[as.integer(str_remove(cur_column(),'TB'))],
cumsum(pmin(,,cc[as.integer(str_remove(cur_column(),'TB'))])))));;)
)) %>%
排列(版次(行号())
#>工厂类型所有权TB1 TB2 TB3 TB4 TB5
#>1吨基地酒吧45432162271478
#>2 S基本酒吧275 75 385 491 60
#>3 Y基地酒吧3144252221363
#>4吨基础酒吧170 122 490 332 123
#>5 J基地酒吧241 178 173 472 468
#>6 B基本酒吧243 316 152 411 434
#>7吨底座127 167 356 451 368
#>8 U基本酒吧20 102 54 182 0
#>9 O基地酒吧211 322 19 103 0
#>10 J基本酒吧0 0 444 0
由(v2.0.0)于2021-05-24年创建,我想从中减去常数c,这样,添加了df[76,13]@RonakShah示例。谢谢。@RonakShah编辑了这个例子。谢谢。@dhruvak_a,没有循环就解决了它,即只遵循dplyr
策略。如果您不想使用stringr
@AnilGoyal,您可以将str_replace替换为gsub/sub。谢谢,代码在前几次运行良好。但现在它将数据集的前几行转换为NA。知道为什么吗?这似乎不是全班的问题。
dt <- data.frame(Plant = sample(LETTERS,10,replace=T),
Type = rep("Base",10),
Ownership = rep("Pub",10))
caps = matrix(round(runif(10*5,0,500),0),nrow=10,ncol=5)
dt <- as.data.frame(cbind(dt,caps)) #this what the data frame looks like
for(i in 1:5){
colnames(dt)[i+3] <- (paste0("TB",i))
}
dt
Plant Type Ownership TB1 TB2 TB3 TB4 TB5
1 T Base Pub 454 32 162 271 478
2 S Base Pub 275 75 385 491 60
3 Y Base Pub 314 44 252 221 363
4 T Base Pub 170 122 490 332 123
5 J Base Pub 241 178 173 472 468
6 B Base Pub 243 316 152 411 434
7 T Base Pub 127 167 356 451 400
8 U Base Pub 20 102 54 182 57
9 O Base Pub 368 333 236 103 27
10 J Base Pub 343 189 0 494 184
c <- c(500,200,217,50,300)
#required output
Plant Type Ownership TB1 TB2 TB3 TB4 TB5
1 T Base Pub 454 32 162 271 478
2 S Base Pub 275 75 385 491 60
3 Y Base Pub 314 44 252 221 363
4 T Base Pub 170 122 490 332 123
5 J Base Pub 241 178 173 472 468
6 B Base Pub 243 316 152 411 434
7 T Base Pub 127 167 356 451 368
8 U Base Pub 20 102 54 182 0
9 O Base Pub 211 322 19 103 0
10 J Base Pub 0 0 0 444 0
#dt[10,4] is now max((343-500),0), while dt[9,4] is 368-(500-343).
#dt[10,5] is now max((189-200),0), while dt[9,4] is 333-(200-189).
#and so on.
for(i in 4:8){
j <- nrow(dt) #start from the last row
if(dt[j,i]>0){
res1 <- c[i] - dt[j,i] #residual value of the difference
dt[j,i] <- max((dt[j,i] - c[i]),0)
while(res1>0){ #the process should continue until an amount equivalent to c[i] is not subtracted from dt[j,i]
j <- j-1
p <- dt[j,i]
dt[j,i] <- max((dt[j,i] - res1),0)
res1 <- res1 - p
}
}
else if(dt[j,i]==0){ #if the last element of the column is already 0, process should start w/ the first non-zero element
j <- j-1
res1 <- c[i] - dt[j,i]
dt[j,i] <- max((dt[j,i] - c[i]),0)
while(res1>0){
j <- j-1
p <- dt[j,i]
dt[j,i] <- max((dt[j,i] - res1),0)
res1 <- res1 - p
}
}
}