matrix - Collapse data frame into single row and creating new columns based on row R -
I have a data frame with an object name and a list of statistical moments for that object, such as:
IQR SCU X 1 1 2 2 2 3 3 3 3 3For every line, I want statistical moments and object names to be preceded by attached. Such as:
xMean xIQR xSkew yMean yIqR ySkew zMean zIQR zSkew 1 1 2 2 2 3 3 3
In short, what do I need in one line Data frame as it lists all statistical moments on a line because I get many lines like the last line but a limited set of columns.
You can:
df1 $ id & lt; - 1 Again the shape (df1, idvar = "id", timevar = "object", direction = "wide") [- 1] # min.x IQR.x Skew.x mean. Using IQR.y Skew.y Mean.z IQR.z Skew.z # 1 1 1 2 2 2 3 3 3
or dcast
, select < Code> melt to
reshape2
library (reshape2) dcast (molten (df1, id.var = c ('id', 'object' ), Id ~ ..., value.var = 'value') [- 1] # x_Mean x_IQR x_Skew y_Mean y_IQR Y_Skew z_Mean z_IQR z_Skew # 1 1 1 2 2 2 3 3 3
Or Usin g dplyr
and tidyr
library (dplyr) library (tidyr) df1% & gt;% collect (wise, val , Pisces: SCU)% Unite% (GC; VarNew, Object, Var, sep = "")% & gt;% Spread (VarNew, Val)% & gt;% (-Id) #xIQR xMean XSkew yIQR yMean ySkew zIQR zMean zSkew # 1 1 1 2 2 2 3 3 3
data < / H3> df1 <- structure (list (object = c ("x", "y", "z"), mean = 1: 3, IQR = 1: 3, scale = 1 : 3). Name = C ("Object", "Pisces", "IQR", "SCU"), class = "data .frame", line.Name = C (NA, -3L))
df1 <- structure (list (object = c ("x", "y", "z"), mean = 1: 3, IQR = 1: 3, scale = 1 : 3). Name = C ("Object", "Pisces", "IQR", "SCU"), class = "data .frame", line.Name = C (NA, -3L))
Comments
Post a Comment