-3

I want to create a matrix like below. Basically I want to check row name is less than less than current column name and greater than previous column name. If condition is true then 1 else o.

          94673160  946764000 946785600  

946702800 1 0 0

946706400 1 0 0

946710000 1 0 0

946713600 1 0 0

946717200 1 0 0

946720800 1 0 0

946724400 1 0 0

946728000 1 0 0

946731600 0 1 0

946735200 0 1 0

946738800 0 1 0

946742400 0 1 0

946746000 0 1 0

946749600 0 1 0

946753200 0 1 0

946756800 0 1 0

946760400 0 0 1

946764000 0 0 1

946767600 0 0 1

946771200 0 0 1

946774800 0 0 1

946778400 0 0 1

946782000 0 0 1

946785600 0 0 1

adding image for reference

  • 3
    poorly formatted question, and vague question. Format the question properly so that we can understand and answer it – Amit Gupta Mar 06 '19 at 03:35
  • Please read [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and edit accordingly. – NelsonGon Mar 06 '19 at 05:17

1 Answers1

0

Although poorly worded question with little to go by, I think I gather what you are trying to achieve. I assume you have two vectors (one to become the colnames and one to become the rownames) and you want a the matrix filled with 1,0 for whether the rowname is greater than the previous position colname and less than the current indexed colnames? The first problem is what to do with the first column, there is no preceding column so I assume we only test for whether is less than the colname name at pos 1.

## Dummy code - ordered to make easier to follow but does not matter
cols <- sample(1:1000,20)
row <- sample(1:1000,20)
cols<-cols[order(cols)]
row<- row[order(row)]

## Do the test for what will be col 1 as it has different rule (no proceeding column)
col1 <- ifelse(row < cols[1], 1,0)
df <- data.frame(row.names = row,col1)
## loop through and test the remaining and cbind into dataframe
for(i in 2:length(cols)){
  tmp<-ifelse(row < cols[i] & row >cols[i-1], 1,0)
 df<-cbind(df, tmp)
}
colnames(df)<-cols
George
  • 903
  • 8
  • 22