How to shift a row by one Column?

Viewed 71

A row shift a column to the right:

 col1 col2 col3 col4 col5
1 A    A    A    A
2 B    B    B    B
3 NA   C    C    C    C
4 D    D    D    D

How can I move C back to its proper place?

I have tried the following but with no luck:

nc  <- ncol(df)
df[3, 1:nc] <- df[3, 2:(nc-1)]
2 Answers

You need to do :

df[3, 1:(nc-1)] <- df[3, 2:nc]

then probably delete col5 :

df$col5 <- NULL

You could use sed to preprocess the data, like:

sed -i 's/NA,//g' data.csv

Considering, data.csv being the original data file

Related