I have a data frame that looks similar to this (I've cut some out for easier reference, data has 93 rows):
Rank 1 A B C D
34 (TPE) 2 4 6 12
35 (TUR) 2 2 9 13
36 (GRE) 2 1 1 4
(UGA) 2 1 1 4 <NA>
I need to have the columns line up, but some of the data in "Rank" is offset to the left one column. I have assigned the rows with this problem to a vector: off.set.rows <- c(which(is.na(df[ , 6]))) I need to have all rows in that vector shift one column to the right and replace the empty space it leaves in column 1 with the number in column 1 in the row previous to it. It should look like this:
Rank 1 A B C D
34 (TPE) 2 4 6 12
35 (TUR) 2 2 9 13
36 (GRE) 2 1 1 4
36 (UGA) 2 1 1 4
I've tried this: df[off.set.rows, 1:(ncol(df))] <- df[off.set.rows, 2:(ncol(df))], but it shifts everything in the row left one column and the (UGA) disappears, it moves the to column 5 and then repeats the value that moves into column 2 again in column 6 like this:
Rank 1 A B C D
34 (TPE) 2 4 6 12
35 (TUR) 2 2 9 13
36 (GRE) 2 1 1 4
2 1 1 4 <NA> 2
Help is much appreciated!!