dataframe - R: aggregate columns of a data.frame -


I have a data frame that looks like this

  & gt; Head (DF) Memory Memory Memory naive inexperienced 10472501 6.075714 5.898929 6.644946 6.023901 6.332126 8.087944 7.520194 10509163 6.168941 6.495393 5.951124 6.052527 6.404401 7 , 152890 8.335509 10496091 10.125575 9.966211 10.075613 10.310952 10.090649 11.803949 11.274480 10427035 6.644921 6.658567 6.569745 6.499243 6.990852 8.010784 7.798154 10503695 8,379494 8,153917 8,246484 8,390747 8,346748 9,540236 9.0 9 1740 10451763 10.986717 11.233819 10.643245 10.230697 10.541396 12.248487 11.823138  

I Memory Meaning of the column and the meaning of the naive. Columns Total Function rows aggregate This is data.frame possibly a large number Security may be lines, and so the original assumes data Gross colnames can be transposing being implemented by .frame I feel bad, and Generally disturbed:

  & gt; (T (Total (T (D (D), List (Colnames (DF)), mean))) [1] [2] Group 1 "Memory" "Navek" 10472501 "6.195123" "8.125439" 1050 9616 "6.214477" 7.733625 "10496091" 10.1380 "" 11.55348 "10427035" 6.672665 "" 8.266854 "10503695" 8.303478 "is coming" 9.340436 " 

indiscriminate clearly remember me?

I am a big advocate for reproducing the data so that it is in the "tall" format. The usefulness of this is particularly evident when it comes to similar problems. Fortunately, It's easy to resemble data in almost any format with the reshape package.

If I think my question is correct, then you memory and unnecessary for any reason for each line, we need to make the column names unique to reshape :: melted () . Colnames ( Df)

Then, you get a id Create columns. You can either

  df $ ID & lt; - 1: nrow (df)  

Or, if those rownames are meaningful

  df $ ID & lt; - rownames (df)  

Now, with reshape package

  library (resize) df.m & lt ; - Melt (DF, ID = "ID") df.m  

df.agg should now look like your desired output snippet.

Or, if you only want the overall tool in all the rows, the suggestion of Jack will work

  m < - ColMeans (df) tapply (m, colnames (df), mean)  

You can get the same result, but

  cast (df Is formatted as a dataframe with .m, ~ variable, fun = mean)  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -