Tag: R

Flatten Data in R by Column Values

r-csv

Not sure if this will help anyone, but I recently wrote a script that will let you flatten/widen a data set based on a dynamically set column name.


Animal Instances Instances Instances Instances
Puppy 47 77 97 25
Puppy 26 6 14 98
Kitten 38 50 5 73
Puppy 83 61 43 4
Kitten 78 30 57 8

…would turn into this


V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
Puppy 47 77 97 25 26 6 14 98 83 61 43 4
Kitten 38 50 5 73 78 30 57 8

Breaks a vector into defined segment lengths

Another gist written for stack overflow. This breaks up a vector defined lengths and accounts for the remainder size. Great for splitting up a vector for saving into different files. It is a loop, but any performance issues should be minimal and increases readability/extendability.