I am cleaning a big dataset, that contains dates (t0:t5 and no_BL:no_48) and test scores (MMSE_BL: MMSE_FU48).
LONG STORY SHORT, I need this date: "27-09-12", to look like this "27-09-12_FU24_28",
- where the _FU24 come from a header (columns no_BL : no_FU48).
- and _28 comes from a score value (columns MMSE_BL_MMSE_FU48).
Longer explanation: In this example, I am showing my first 2 rows and 18 columns. Probably the best way to tell you my goal is to show how I would like the outcome to look like:
### What I have
cnames <- c("t0", "t1", "t2", "t3", "t4", "t5", "no_BL", "no_FU6", "no_Fu12", "no_FU24", "no_FU36", "no_FU48", "MMSE_BL","MMSE_FU6" ,"MMSE_FU12", "MMSE_FU24", "MMSE_36", "MMSE_FU48")
one <- c("27-09-12", "NA", "25-07-13", "NA", "NA", "NA", "NA", "NA", "NA", "27-09-12", "25-07-13", "NA", "29", "NA", "28", "28", "29", "30")
two <- c("06-11-12", "NA", "01-10-13", "NA", "NA", "NA", "NA", "NA", "NA", "06-11-12", "01-10-13", "NA", "30", "NA", "29", "30", "30", "30")
df <- matrix(c(one, two), ncol = 18, byrow = TRUE )
colnames(df) <- c(cnames)
df <- as.data.frame(df)
View(df)
##### What I need
#OUTCOME
one_ <- c("27-09-12_FU24_28", "NA", "25-07-13_FU36_29", "NA", "NA", "NA", "NA", "NA", "NA", "27-09-12", "25-07-13", "NA", "29", "NA", "28", "28", "29", "30")
two_ <- c("06-11-12_FU24_30", "NA", "01-10-13_FU36_30", "NA", "NA", "NA", "NA", "NA", "NA", "06-11-12", "01-10-13", "NA", "NA" ,"30", "29", "30", "30", "30")
outcome <- matrix(c(one_, two_), ncol = 18, byrow = TRUE )
colnames(outcome) <- c(cnames)
outcome <- as.data.frame(outcome)
View(outcome)
It is a 2 step proccess,
First I would like to take the dates contained in columns t0:t5, go through columns no_BL:no_48, find their date match, and attach to the dates from t0:t5 columns the last characters of its date-belonging header.
- For example, "27-09-12" from column t0, is also found in column no_FU24, so I need to attach the last 5 digits of the heather(no_FU24) to the date, looking like this = 27-09-12_FU24.
Second after all the first 6 columns are settled, I have to start an iteration again, but this time attaching the scores to the end of the dates.
- For example, "27-09-12_FU24" ends with _FU24, which means that from the column MMSE_FU24 I have to copy the score (28) and attach it to the date value, looking like this at the end = "27-09-12_FU24_28"
I was also contemplating the idea of Header=False, and work with it as a regular row.
Please help! and thank you in advance : )