Home > database >  How to find the values and the corresponding column of those values in a matrix in R
How to find the values and the corresponding column of those values in a matrix in R

Time:02-16

So on a small scale, let's say I have 5 patients which are the rows of a matrix. In the columns, I have some information like age, sex etc. But the question is on some medications. so let's say I have 4 categorical medications. 1 means the patients received them and 0 otherwise. now for some patients I have this vector (1,1,?,?) meaning that the first two values SHOULD be 1 and I know them, but I don't know what are the "?" for each patient.

I assume it should be a matrix of 5 rows and at least 4 columns. now, how can I find the values of "?" for each row? Also, it is SO important to know that this value belongs to which treatment. (I'm not sure if this part of the question makes sense but I also want to know whether for example (1,1,0,1) was the most repeated combination or (1,1,0,0) was or ( 1,1,1,0) or...

I have almost zero knowledge on stat or math but here is the situation and I would appreciate if someone can explain how I can do it in R.

CodePudding user response:

How about something like this, where n is the number of observations that have each pattern:

library(dplyr)
dat <- data.frame(patient = LETTERS[1:20], 
                d1 = 1, 
                d2 = 1, 
                d3 = sample(c(0,1), 20, replace=TRUE), 
                d4 = sample(c(0,1), 20, replace=TRUE))

dat %>% 
  group_by(d1, d2, d3, d4) %>% 
  tally()
#> # A tibble: 4 × 5
#> # Groups:   d1, d2, d3 [2]
#>      d1    d2    d3    d4     n
#>   <dbl> <dbl> <dbl> <dbl> <int>
#> 1     1     1     0     0     1
#> 2     1     1     0     1     6
#> 3     1     1     1     0     9
#> 4     1     1     1     1     4

Created on 2022-02-15 by the reprex package (v2.0.1)

  • Related