I have a data where I would like to compare the measurements two-by-two by taking the difference and say if the difference is more than 0.2 add another column as Yes, otherwise No. Like the example data in the image. And, at the end for each individual if there is at least one Yes, the final is Yes. I have filled the first row manually, but I would like to do the same for all my individuals(1000 individuals).enter image description here
library(readxl)
dd <- read_excel("dd.xlsx")
dd <- dput(dd)
structure(list(ID = c(1, 2, 3, 4, 5, 6), m1 = c("2.1", "1.4","NA", "4.0", "2.5", "NA"),
m2 = c("2.8", "1.5", "NA", "4.0", "3.8", "1.1"),
m3 = c("3.5", "1.5", "NA", "4.0", "NA", "1.3"),
m4 = c("NA", "1.8", "1.8", "4.0", "NA", "1.3"),
m5 = c("NA","1.5", "2.9", "NA", "NA", "1.5"),
m6 = c("NA", "NA", "3.5", "NA", "NA", "1.2")),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L))
This approach uses a pivot longer to estimate your columns, and then pivot wider with rename will give you what you want (See second solution). However, if you just want the
Finalcolumn, by ID, just do this:Finalby IDOutput: