I'm struggling to try to insert NAs columns in specific positions of a data frame.
For instance, I have the dataset:
dataset <- data.frame(c1 = 1:5,
c2 = 2:6,
c3 = 3:7,
c4 = 4:8,
c5 = 5:9,
c6 = 10:14,
c7 = 15:19,
c8 = 20:24,
c9 = 25:29,
c10 = 30:34)
I'd like to insert, in this example 4 NAs columns after each 2 existent columns of dataset
. The answer would be something like:
dataset.answer <- data.frame(c1 = 1:5,
c2 = 2:6,
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c3=3:7,
c4=4:8,
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c5=5:9,
c6=10:14,
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c7=15:19,
c8=20:24,
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c.und.1<-rep(NA,dim(dataset)[1]),
c9=25:29,
c10=30:34)
Any suggestion of an elegant way to to it?