Monday, 7 September 2015

R coding: skip top "n" row


Skip the first "n" rows in the data

 class(iris)  
 skipfirst <-3  
 finalOutput <- iris[-(1:skipfirst),]  
 write.csv(finalOutput, file = "modified.csv")  

R coding: Shifting one column values upward


The following snippet shows how to shift one of the column values upward  by "n" rows.


 class(iris)
 #move 10 rows forward on SepalLength
 shiftBy <- 10
 sepalLenCol <- iris$Sepal.Length
 sepalLenCol <- (tail(sepalLenCol,-shiftBy))
 len <- (nrow(iris)-shiftBy)
 finalOutput <- iris[(1:len),]
 finalOutput$Sepal.Length <- sepalLenCol
 write.csv(finalOutput, file = "modified.csv")