I found the script below for cleaning NAs and left-shifting rows. Here's the discussion: Dropping all left NAs in a dataframe and left shifting the cleaned rows
for (i in 1:nrow(dat)) {
if (is.na(dat[i,1])==TRUE) {
dat1 <- dat[i, min(which(!is.na(dat[i,]))):length(dat[i,])]
dat[i,] <- data.frame( dat1, t(rep(NA, ncol(dat)-length(dat1))) )
}
}
dat
I'd like to clean a varying number of NAs from the top of columns and then upshift the cleaned columns. Is there any chance someone could help me modify the above script for that purpose?