3

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)]
LD-DS-00
  • 315
  • 2
  • 8

2 Answers2

4

You need to do :

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

then probably delete col5 :

df$col5 <- NULL
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

You could use sed to preprocess the data, like:

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

Considering, data.csv being the original data file

Rohith
  • 87
  • 1
  • 7
  • That doesn't seem to be a `R` related answer, maybe I'm wrong on this. Please give some context to your answer. What is `sed`? In which package it is included? – Martin Gal Jul 16 '20 at 07:32
  • sed is for 'stream editor', an UNIX utility. – Rohith Jul 16 '20 at 07:47