[R] Comparing factor level measurments

jim holtman jholtman at gmail.com
Sat Jun 30 05:10:00 CEST 2012


does this work for you:

> x <- read.csv(text = "Patient, Cycle, Variable1, Variable 2
+ A,1,4,5
+ A,2,3,3
+ A,3,4,NA
+ B,1,6,6
+ B,2,NA,6
+ C,1,6,5
+ C,3,2,2", as.is = TRUE)
>
> # test each Patient to see if columns need to be set to NA
> newX <- do.call(rbind,
+     lapply(split(x, x$Patient), function(.pat){
+         # test each column
+         for (i in c("Variable1", "Variable.2")){
+             # browser()
+             if (any(is.na(.pat[[i]]))) .pat[[i]] <- NA
+         }
+         .pat  # return value
+     })
+ )
> newX  # print result
    Patient Cycle Variable1 Variable.2
A.1       A     1         4         NA
A.2       A     2         3         NA
A.3       A     3         4         NA
B.4       B     1        NA          6
B.5       B     2        NA          6
C.6       C     1         6          5
C.7       C     3         2          2



On Fri, Jun 29, 2012 at 5:03 PM, Lib Gray <libgray3827 at gmail.com> wrote:
> Hello, I have a data set where there are multiple "cycles" per "patient,"
> and I want to exclude from my data set instances where a variable was not
> measured every cycle. The difficulty is that the patients have different
> cycles; some have cycles 1,2, and 3, others only have 1 and 3 (and
> everything in between). Therefore, I'm having difficulty in in
> distinguishing between the NA of not having the cycle, and the NA of not
> having that particular measurement.
>
> Here's a similar example of the data:
>
> Patient, Cycle, Variable1, Variable 2
> A, 1, 4, 5
> A, 2, 3, 3
> A, 3, 4, NA
> B, 1, 6, 6
> B, 2, NA, 6
> C, 1, 6, 5
> C, 3, 2, 2
>
> So in this case, I would want all of Variable 1 for Patient B to be NA, and
> all of Variable 2 for Patient A to be NA.
>
> Does anyone have a good way of tackling this problem? I'm having issues
> with trying to subset the data such that I can compare Cycles within the
> patient's factor level.
>
> Thanks!
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list