0

I am trying to split a column of numerical data by allocating every 12 consecutive values (non-repetitive) into a new (shorter) column. Any help would be greatly appreciated.

So far I tried:

    df_all <- rnorm(500, mean = 5, sd = 1)  # example data, I didnt use this one
    df_final <- matrix(, nrow = 12, ncol = 0)
    
    for(i in seq(1, length(df_all), 12)) {
      x <- df_all[i:i+11]
      df_final <- data.frame(cbind(df_final, x))
    }

    write.csv(df_final, file = paste0(rootdir,"/","mT",'.csv'))

It doesnt work, it is appending the same 12th values of segment "x" to all 12 rows of the new column. I checked df_all[1:12] and it is returning normal correct values.

enter image description here

y chung
  • 103
  • 4
  • 1
    Could you provide a [minimum reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data? – Rory S Jun 18 '21 at 14:45
  • Technically a csv file with a column of numerical values, it doesnt matter what those values exactly are, it can be all 1, I just want it to split into multiple columns, each with a max row length of 12. – y chung Jun 18 '21 at 14:57
  • If we want to generate ideas and test solutions, we will do so with example data. It's good practice to provide that data and not expect us to generate it ourselves. – ktiu Jun 18 '21 at 17:46
  • obviously, I am new here and in r, you can just create a long list of numbers any way you want...I dont know what's the fuss about it to make it so newbie unfriendly – y chung Jun 18 '21 at 17:54
  • 2
    Your bug was `[i:(i + 11)]` – ktiu Jun 18 '21 at 18:24

1 Answers1

2

You could do

df_all |>
  matrix(nrow = 12) |>
  as.data.frame()

returning (truncated):

         V1       V2       V3       V4       V5       V6       V7       V8
1  3.583938 6.071985 6.429908 4.632524 5.477596 4.548784 4.588537 5.487138
2  4.055486 5.828313 3.517980 7.242400 4.909442 6.149804 3.990672 6.752652
3  3.710380 4.001795 5.739687 3.479396 6.191938 5.578237 3.252641 6.070115
4  4.867136 5.191307 5.884153 4.647208 5.437753 3.455165 5.989859 7.442414
5  5.111246 5.882136 6.361230 5.186235 3.867077 5.631055 5.030219 4.595383
6  5.061778 5.560719 4.146807 5.341364 2.919934 3.305795 5.129994 6.159989
7  6.188377 4.606850 6.693071 6.997215 5.722066 5.307161 4.795347 5.122470
8  4.035036 4.785901 3.325937 4.907982 5.490938 6.416011 7.522089 6.845924
9  6.152382 3.506762 4.541983 4.166833 3.461948 4.155356 2.969548 4.585297
10 6.834143 5.818803 5.048908 2.944199 4.999610 4.892196 4.242976 4.337777
11 4.293846 4.744218 4.244750 4.261823 4.552857 3.965914 4.560048 4.257186
12 5.605399 6.246158 6.622988 7.637657 5.349231 4.258165 4.357449 5.635602

Data used:

df_all <- rnorm(500, mean = 5, sd = 1)
ktiu
  • 2,606
  • 6
  • 20