1

I have a data frame like this:

V1  V2 V3       V4        V5   V6 V7     V8    V9     V10   V11     V12 V13 V14 V15 V16 V17   V18 V19   V20  V21 Date
1 Thu Jul  7 11:59:18 201611968 9984  0 9973.4 99904 36999.0 40960 24086.3   -   -   -   -  15 0.145   1 0.025 0.17 <NA>
2 Thu Jul  7 11:59:38 201611968 9984  0 9973.4 99904 39544.0 40960 24086.3   -   -   -   -  15 0.145   1 0.025 0.17 <NA>

I need to split column V5 into two columns. 2016 part is the year and I need to put that into another column itself? Any ideas how I could do this?

user1471980
  • 10,127
  • 48
  • 136
  • 235

1 Answers1

1

We can use sub to create a delimiter (,) between the 4th and 5th character in 'V5', use read.csv to read it as data.frame and cbind with the original dataset.

cbind(df1, read.csv(text=sub('(.{4})(.*)', "\\1,\\2", df1$V5), header=FALSE))
akrun
  • 874,273
  • 37
  • 540
  • 662