if statement - R: Automated ifelse calculation in matrix for every row -
for large matrix [n,1], want find out every row, if value smaller 0.1 , if following value (in following row) bigger 0.1.
ifelse(matrix[1,1]<0.1 & matrix[2,1]>0.1, "1", "0") ifelse(matrix[2,1]<0.1 & matrix[3,1]>0.1, "1", "0") ifelse(matrix[3,1]<0.1 & matrix[4,1]>0.1, "1", "0")`
how can automate calculation every row?
this vectorized:
set.seed(6l); n <- 10l; m <- matrix(rnorm(n,0.1,0.01),ncol=1l); m; ## [,1] ## [1,] 0.10269606 ## [2,] 0.09370015 ## [3,] 0.10868660 ## [4,] 0.11727196 ## [5,] 0.10024188 ## [6,] 0.10368025 ## [7,] 0.08690796 ## [8,] 0.10738622 ## [9,] 0.10044873 ## [10,] 0.08951603 m[-length(m)]<0.1 & m[-1l]>0.1; ## [1] false true false false false false true false false
if want actual row indexes:
which(m[-length(m)]<0.1 & m[-1l]>0.1); ## [1] 2 7
Comments
Post a Comment