I'm working with data whereby gender of participants has been noted in three columns ( baseline, first time point and second time point). For several hundred participants they have gender for baseline and second time point which hasn't changed, but NA at first time point.
I'm trying to derive correct code to change the NA in this column, if the same value (1=male, 2=female) is present in the columns before (baseline) and after it (second time point). My knowledge is basic as I'm learning R currently.
Please could anybody suggest some code that might work? I'm trying to figure out a for loop with if/else statements, but not quite getting there.
df$tp1 is first time point, df$base is baseline, df$tp2 is second time point
for(i in df$tp1) {
if (df$base == df$tp2)
df <- replace(df$tp1, df$tp1 =="NA", df$base)
else (df$tp1 == df$tp1)}
print(df)
Edit: Please find below an example of the data
Baseline TP1 TP2
1 NA 1
1 1 1
2 NA 2
1 1 1
1 1 1
2 2 2
In row 1 and row 3, I would like to be able to change the NA to the same number as baseline and second timepoint, i.e 1 and 2 respectively in these rows.
dput(head(gender_only, 15))
structure(list(Baseline = c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L), `First-timepoint` = c(NA, 1L, NA, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), `second-timepoint` = c(1L,1L, 2L, NA, 1L, 1L, NA, 1L, 1L, 1L, 1L, 1L, 2L, 2L, NA)), row.names = c(NA,
15L), class = "data.frame")