r - Calculate a new matrix with multiple possible outcomes from pre-existing variables -
i need overlay 2 large matrices (or columns merger of two) generate final matrix analysis. have series of points counts made 3 times per year number of year (generating e.g. counts 2000_1, 2000_2, etc.), point wasn’t done 1 reason or another. if point done, there may have been detections or there may not. there 3 possible results each cell in final matrix calculated other 2 values. if no sampling occurred @ point on occasion, need value “.”; if sampling occurred no detections occurred, need “0”, , if sampling occurred , 1-n individuals detected, need “1”.
in sample data below, i’ve combined 2 matrices simplify note actual datasets 750 records on 64 columns. in column 1, point id provided. next 5 columns (pt.x) provide whether count conducted in given year_rotation, , last 5 how many if detections. na no data, , should occur when sampling didn’t occur, happen if there sampling no 0 entered.
sample
point <- c(“a194”,” a234”,”a83”,” k37”,” ts47”) p0.1 <- c(1,1,1,1,0) p0.2 <- c(1,1,1,1,0) p0.3 <- c(1,1,1,1,0) p1.1 <- c(1,0,1,0,0) p1.2 <- c(1,0,1,1,0) d0.1 <- c(1,0,0,3,na) d0.2 <- c(0,0,2,2,na) d0.3 <- c(0,0,0,1,na) d1.1 <- c(0,na,0,na,na) d1.2 <- c(0,na,0,0,na)
and here's i'd end with:
point f0.1 f0.2 f0.3 f1.1 f1.2 a194 1 0 0 0 0 a234 0 0 0 . . a83 0 1 1 0 0 k37 1 1 1 . 1 ts47 . . . . .
can suggest clean, general way transferrable larger dataset or, other similar datasets? can either start scenario, points , detections (p , d columns above) in same dataframe, or 2 frames, 1 each condition. help, , sorry if there format issue post. relatively new formatting such.
i'm not sure how derive result:
- why need
p*
vectors? - why
f1.2
0 . 0 1 .? how last 1?
here attempt (based on i've understood question):
point <- c("a194","a234","a83","k37","ts47") d0.1 <- c(1,0,0,3,na) d0.2 <- c(0,0,2,2,na) d0.3 <- c(0,0,0,1,na) d1.1 <- c(0,na,0,na,na) d1.2 <- c(0,na,0,0,na) #create matrix whatever start d <- ls(pattern="d\\d+\\.\\d+") m <- do.call(cbind, mget(d)) rownames(m) <- point res <- (m>0)+0 colnames(res) <- gsub("d", "f", d)
i think want print na
values .
? don't understand why you'd want that, achieves it:
#create class print method prints na . class(res) <- c("myclass", class(res)) print.myclass <- function(x) { x[is.na(x)] <- "." print.data.frame(as.data.frame(x)) } res # f0.1 f0.2 f0.3 f1.1 f1.2 # a194 1 0 0 0 0 # a234 0 0 0 . . # a83 0 1 0 0 0 # k37 1 1 1 . 0 # ts47 . . . . .
Comments
Post a Comment