r - Use describe function by another variable -
for sample data frame:
df1 <- structure(list(x = structure(1:9, .label = c("a", "b", "c", "d", "e", "f", "g", "h", "i"), class = "factor"), col.1 = c(2.4, 5.6, 7.4, 3.5, 31.2, 2, 7.9, 5, 17.8), col.2 = structure(c(1l, 1l, 2l, 2l, 2l, 1l, 2l, 2l, 1l), .label = c("cat", "dog"), class = "factor")), .names = c("x", "col.1", "col.2"), class = "data.frame", row.names = c(na, -9l ))
i wish create summary stats col.1 column. whilst use:
library(psych) describe(df1$col.1)
i want instead create summary stats used in describe
col.2 (this characters in example factors in real data). example have summary stats cat , dog.
you can conditional analysis tapply
:
tapply(df1$col.1, df1$col.2, describe) # $cat # vars n mean sd median trimmed mad min max range skew kurtosis se # 1 1 4 6.95 7.41 4 6.95 2.67 2 17.8 15.8 0.65 -1.77 3.71 # # $dog # vars n mean sd median trimmed mad min max range skew kurtosis se # 1 1 5 11 11.43 7.4 11 3.56 3.5 31.2 27.7 1.01 -1 5.11
Comments
Post a Comment