r - dividing a long column to many short ones by a condition -


i have long column of numbers. want r make new column every time value changes zero. example column:

90.1194354  87.94788274  80.34744843  64.06080347  30.40173724  0  0  0  0  0  16.28664495  23.88707926  29.31596091  48.85993485  13.02931596  0  0  0  7.600434311  20.62975027  29.31596091  32.5732899  

for example want 3 columns.

thanks!

i don't know how expect have columns of different lengths. pad them na?

this solution uses rle , splits vector list:

temp <- c(90.1194354, 87.94788274, 80.34744843, 64.06080347, 30.40173724,            0, 0, 0, 0, 0, 16.28664495, 23.88707926, 29.31596091, 48.85993485,            13.02931596, 0, 0, 0, 7.600434311, 20.62975027, 29.31596091,            32.5732899)  x <- rle(temp == 0) split(temp, rep(seq_along(x$lengths), times = x$lengths))[c(true, false)] # $`1` # [1] 90.11944 87.94788 80.34745 64.06080 30.40174 #  # $`3` # [1] 16.28664 23.88708 29.31596 48.85993 13.02932 #  # $`5` # [1]  7.600434 20.629750 29.315961 32.573290 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -