using DataFrames,Pipe
x = DataFrame(id=[1,2,3,4], id2=[1,1,2,2], M1 = [11,12,13,14], M2 = [111,112,113,114])
Stack a data frame x, i.e. convert it from wide to long format. Stack(df, [아래로 펼칠 컬럼], 표시할 컬럼)
stack(x,[:M1,:M2],:id) # first pass measure variables and then id-variable
add view=true
arguement to make a view; in that case columns of the resulting data frame share memory with columns of the source data frame, so the operation is potentially unsafe.
# optionally you can rename columns
stack(x,["M1","M2"], "id", variable_name="key", value_name="observed",
view=true)
if second argument is omitted in stack
, all other columns are assumed to be the id-variables
stack(x,[:M1,:M2])
stack(x,Not([:id,:id2]))
stack(x,Not([1,2]))
x = DataFrame(id=[1,1,1],id2=['a','b','c'],a1=rand(3),a2=rand())
x = DataFrame(id=[1,1,1],id2=['a','b','c'],a1=rand(3),a2=rand(3))
if `stack` is not passed any measure variables by default numeric variables are selected as measures
stack(x)
here all columns are treated as measures:
stack(DataFrame(rand(3,2),:auto))
df = DataFrame(rand(3,2),:auto)
df.key = [1,1,1]
df
mdf = stack(df) # duplicates in key are silenetly accepted
x = DataFrame(id=[1,1,1],id2='a':'c',a1=rand(3),a2 = rand(3))
y = stack(x)
unstack(y,:id2,:variable,:value)
unstack(y,:variable,:value) # all other columns are treated as keys
all columns other than named :variable
an :value
are treated as keys
unstack(y)
# you can rename the unstacked columns
unstack(y,renamecols=n->string("unstacked_",n))
df = stack(DataFrame(rand(3,2),:auto))
unable to unstack when no key column is presented
unstack(df)