Reputation: 188
I am trying to build an iterative logic to achieve something but I am somehow failing in doing so:
I have the following data
Identifier A B C D E F G H I
--------- --- -- -- -- -- -- -- -- --
1 0 0 0 0 0 0 1 0 0
2 0 0 1 0 1 0 0 1 1
3 0 0 0 0 0 0 1 1 1
I want to replace all the zeros occurring before the first 1 with "N" in each row and leave the rest of the 0's and 1's as it is.
This is the desired output
Identifier A B C D E F G H I
--------- --- -- -- -- -- -- -- -- --
1 N N N N N N 1 0 0
2 N N 1 0 1 0 0 1 1
3 N N N N N N 1 1 1
I was trying out this code but it did not give me desired results
for (i in 1:nrow(test1)) {
for (j in 2:9){
if(test1[i,j]==0){
k <- i
}
for(l in 1:k){
test1[l,j]=3
}
}
}
Can anyone help me out here?
Upvotes: 2
Views: 50
Reputation: 11981
since you did not mention your data format I will provide an answer for matrices:
You can use the apply
function like this:
m <- matrix(c(0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,1,1,0,0,0,0,0,0,1,1,1), 3, 9, byrow = T)
myfun <- function(x) {
idx = min(which(x == 1))-1
x[1:idx] <- "N"
return(x)
}
t(apply(m, 1, myfun))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "N" "N" "N" "N" "N" "N" "1" "0" "0"
[2,] "N" "N" "1" "0" "1" "0" "0" "1" "1"
[3,] "N" "N" "N" "N" "N" "N" "1" "1" "1"
Upvotes: 2
Reputation: 887098
We can use apply
df1[-1] <- t(apply(df1[-1], 1, function(x) replace(x, cumsum(x == 1) ==0, "N")))
Or using rowCumsums
from matrixStats
library(matrixStats)
df1[-1][rowCumsums(as.matrix(df1[-1])) == 0] <- "N"
df1
# Identfier A B C D E F G H I
#1 1 N N N N N N 1 0 0
#2 2 N N 1 0 1 0 0 1 1
#3 3 N N N N N N 1 1 1
df1 <- structure(list(Identfier = 1:3, A = c(0L, 0L, 0L), B = c(0L,
0L, 0L), C = c(0L, 1L, 0L), D = c(0L, 0L, 0L), E = c(0L, 1L,
0L), F = c(0L, 0L, 0L), G = c(1L, 0L, 1L), H = c(0L, 1L, 1L),
I = c(0L, 1L, 1L)), class = "data.frame", row.names = c(NA,
-3L))
Upvotes: 4