[R] PrettyR (describe)

Chuck Cleland
Thu, 19 Jun 2008 18:21:10 -0000

On 6/19/2008 1:54 PM, stephen sefick wrote:
> #is there a way to get NA in the table of descriptive statistics instead of
> the function stopping  Thank you in advance

   Replace sd() with something like this:

mysd <- function(x, na.rm=TRUE){ifelse(all(is.na(x)), NA, sd(x,
na.rm=TRUE))}

   Or consider dropping the problem variable like this:

describe(x.f[,colMeans(is.na(x.f)) < 1], num.desc=c("mean",
"median",
"sd", "min", "max", "skewness",
"Q25", "Q75","valid.n"))

> #data
> x.f <- structure(list(Site = structure(c(9L, 9L, 9L, 9L, 9L, 9L, 9L,
> 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
> 9L, 9L, 9L, 9L), .Label = c("BC", "HC",
"RM119", "RM148", "RM179",
> "RM185", "RM190", "RM198", "RM202",
"RM215", "RM61", "SC"), class >
"factor"),
>     Alkalinity = c(13, 13, 17, 17, 21, 21, 19, 14, 16, 15, 14,
>     14, 14, 15, 15, 9.7, 17, 14, 15, 14, 14, 15, 15, 15, 15,
>     15, 14), Arsenic = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
>     NA, NA, NA, NA, 0.00039, NA, 0.001, NA, 0.00037, 0.00046,
>     0.00026, 0.00033, 0.00059, 3e-04, NA, NA, NA, NA), BOD..5.day = c(NA,
>     NA, NA, 3.3, NA, NA, 2.1, NA, NA, NA, NA, NA, NA, NA, NA,
>     3.4, NA, NA, 2.4, NA, NA, NA, NA, NA, NA, NA, NA), Cadmium = c(NA,
>     NA, 0.00021, 5.3e-05, NA, NA, NA, 0.0036, NA, NA, NA, NA,
>     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
>     ), Calcium = c(2.3, 1.9, 2.3, 2, 2.2, 2, 2.5, 2, 2.2, 2,
>     1.7, 1.8, 1.8, 2.2, 2.1, 3.4, 2.2, 2.2, 2.6, 2.4, 2.6, 2.5,
>     2.1, 2.4, 2.3, 2.5, 2.6), Carbonaceous.BOD..5.day = c(NA_real_,
>     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
>     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
>     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
>     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
>     NA_real_, NA_real_), Chloride = c(2.9, 2.5, 2.6, 3.4, 2.5,
>     2.5, 2.4, 2.4, 2.7, 3, 2.8, 2.9, 2.8, 3.2, 3.1, 2.7, 2.9,
>     3, 3.1, 2.9, 2.8, 2.8, 2.8, 2.8, 2.9, 2.9, 3.1), Chromium = c(0.00035,
>     NA, NA, NA, NA, 0.00047, NA, 0.00041, 0.00088, NA, 0.00089,
>     0.0017, 0.00087, 0.00052, 0.00068, 0.021, 0.0016, 0.00086,
>     0.002, 0.001, 0.00098, 0.00069, 0.00058, NA, NA, 0.0032,
>     0.00081), COD = c(NA, NA, 12, 11, NA, NA, 7.9, 6.3, NA, NA,
>     9.3, 14, NA, 8.8, 6.9, 48, NA, 9.1, NA, NA, 11, NA, NA, 13,
>     NA, 20, NA), Copper = c(NA, 0.00023, 9e-04, 0.0046, NA, 0.00079,
>     0.00095, 0.00082, 0.00052, 0.00078, 0.00074, 0.0012, 0.00087,
>     0.0013, 0.001, 0.008, 0.00093, 0.00081, 0.0011, 0.00058,
>     0.001, 0.0011, 8e-04, 0.00094, 0.00047, 0.0011, 0.0011),
>     Dissolved.Ammonia...N..phenate. = c(NA, 0.14, 0.13, 0.22,
>     NA, NA, NA, 0.085, NA, 0.098, NA, 0.055, NA, NA, NA, 0.14,
>     0.057, 0.087, 0.078, 0.081, 0.086, NA, 0.07, NA, 0.11, NA,
>     NA), Dissolved.Nitrate.Nitrite...N = c(0.13, 0.15, 0.11,
>     0.14, 0.13, 0.15, 0.16, 0.12, 0.054, 0.055, 0.051, 0.086,
>     0.098, 0.13, 0.13, 0.1, 0.16, 0.13, 0.15, 0.13, 0.14, 0.1,
>     0.035, 0.22, 0.058, 0.054, 0.17), Dissolved.Nitrite...N = c(NA,
>     NA, NA, NA, NA, NA, NA, 0.0076, 0.0057, NA, NA, NA, NA, NA,
>     NA, NA, NA, NA, NA, NA, NA, 0.0067, NA, 0.012, 0.017, NA,
>     0.005), DOC = c(4.6, 2.4, 2.7, 3.8, 2.4, 2.4, 2, 2.4, 2.2,
>     2.2, 2.4, 2.4, 2.3, 2.9, 2.7, 14, 2.4, 2.5, 3, 3, 2.9, 2.6,
>     2.5, 2.5, 2.4, 2.5, 2.2), Iron = c(0.26, 0.21, 0.099, 0.086,
>     0.1, 0.082, 0.04, 0.087, 0.097, 0.064, 0.22, 0.23, 0.12,
>     0.3, 0.23, 9.8, 0.35, 0.13, 0.23, 0.15, 0.09, 0.071, 0.073,
>     0.16, 0.074, 0.24, 0.14), Lead = c(0.00024, 8.2e-05, NA,
>     0.00031, NA, 3.2e-05, 1.9e-05, 0.00025, 6.5e-05, 4.3e-05,
>     2e-04, 0.00026, 7.8e-05, 0.00013, 0.00015, 0.0072, 2e-04,
>     5.7e-05, 0.00013, 0.00012, 9.9e-05, NA, 5.6e-05, NA, NA,
>     0.00014, 5.3e-05), Magnesium = c(1.3, 1.2, 1.3, 1.5, 1.3,
>     1.1, 1.4, 1.2, 1.4, 1.4, 1.4, 1.5, 1.3, 1.5, 1.3, 2.1, 1.4,
>     1.4, 1.5, 1.4, 1.5, 1.4, 1.3, 1.4, 1.3, 1.5, 1.4), Manganese = c(0.028,
>     0.11, 0.034, 0.043, 0.088, 0.09, 0.068, 0.091, 0.081, 0.079,
>     0.24, 0.24, 0.1, 0.076, 0.055, 1.8, 0.096, 0.044, 0.18, 0.076,
>     0.063, 0.063, 0.073, 0.13, 0.096, 0.19, 0.045), Mercury = c(NA,
>     NA, NA, NA, NA, NA, NA, 0.00013, NA, NA, NA, NA, NA, NA,
>     NA, 7.2e-05, NA, 9e-05, NA, NA, NA, NA, NA, NA, NA, NA, NA
>     ), Nickel = c(0.00051, NA, 0.00058, 0.00077, NA, 0.00039,
>     NA, NA, 0.00057, 0.00028, 0.00062, 0.0014, 0.00066, 0.00074,
>     0.00073, 0.011, 0.0012, NA, 0.00086, NA, 0.00038, 0.00038,
>     0.00032, NA, NA, 0.0018, 0.00034), Ortho.phosphorus = c(0.035,
>     0.019, 0.005, 0.03, 0.085, 0.028, 0.014, 0.025, 0.052, 0.011,
>     0.061, 0.21, 0.017, NA, 0.006, 0.037, 0.022, 0.012, 0.035,
>     0.023, NA, NA, 0.019, 0.008, NA, NA, NA), pH = c(7.32, 7.07,
>     7.17, 6.24, 7.12, 7.1, 7.14, 6.94, 7.15, 6.46, 6.1, 6.61,
>     7.08, 7.25, 6.56, 6.73, 7.1, 6.98, 6.97, 7.01, 7.09, 6.63,
>     6.76, 7.15, 7.03, 7.17, 7.3), Phosphorus = c(0.076, 0.014,
>     0.011, 0.0075, 0.017, 1.4, 0.011, 0.018, NA, 0.0074, 0.012,
>     0.012, 0.016, 0.013, 0.019, 0.3, 0.021, 0.012, 0.01, 0.017,
>     0.011, 0.013, 0.011, 0.014, 0.0076, 0.017, 0.011), Potassium = c(1.6,
>     1.6, 2.7, 2.8, 2.2, 2.1, 0.65, 2.3, 2.7, 2.4, 2.8, 2.6, 2.6,
>     2.9, 1.6, 2.8, 2.8, 2.8, 3.3, 2.6, 1.5, 2.4, 1.6, 2.6, 2.3,
>     2.4, 2.4), Selenium = c(4e-04, NA, NA, NA, NA, NA, NA, 4e-04,
>     0.00043, NA, 0.00043, 0.00051, NA, 0.00046, 0.00033, 0.00054,
>     NA, NA, NA, NA, NA, 0.0015, NA, NA, 0.00029, NA, NA), Silicon = c(4.6,
>     4.3, 3.9, 3.5, 4.1, 3.8, 1.6, 4.1, 3.8, 4.4, 4.5, 4.3, 4.1,
>     5, 4.1, 4.9, 5, 4.3, 4.1, 3.6, 4.1, 3.6, 3.6, 3.9, 4.1, 4.1,
>     4.3), Sodium = c(3.2, 3.6, 3.6, 3.8, 2.9, 2.9, 2.8, 3.4,
>     2.9, 2.6, 3.1, 2.8, 2.8, 3.2, 3, 3, 3.1, 3, 4, 3, 2.1, 4,
>     3.8, 3.7, 2.5, 2.7, 3.4), Sulfate = c(3, 2.2, 2.5, 2.2, 2.2,
>     2.2, 2.5, 2.2, 2.4, 2.4, 2.9, 3.4, 2.7, 3.2, 3.5, 4.1, 3.1,
>     3.1, 3.5, 3.3, 3, 2.9, 2.8, 2.7, 2.8, 3.6, 3), TDS = c(42L,
>     46L, 28L, 36L, 29L, 35L, 44L, 33L, NA, 43L, 29L, 42L, 35L,
>     NA, 46L, 78L, 38L, 55L, 32L, 65L, 69L, 43L, 28L, 37L, 57L,
>     29L, 30L), TIC = c(1.5, 1.7, 3.3, 2.2, 2.2, 0.77, 0.79, 2.3,
>     1.9, 2, 2, 2.1, 2.2, 0.91, 0.99, 1.2, 1.6, 2.3, 1.5, 2.2,
>     1.6, 1.4, 1.9, 1.5, 2.1, 3.8, 3.9), TKN = c(0.61, 0.5, NA,
>     0.64, NA, 0.21, 0.25, 0.39, 0.28, 0.44, 0.24, 0.16, NA, 0.76,
>     0.32, 1.7, 0.26, NA, 0.3, 0.24, 0.27, 0.29, 0.17, NA, 0.15,
>     0.089, 0.18), TOC = c(4.3, 2.4, 2.7, 3.9, 2.3, 2.1, 2.1,
>     2.5, 2.2, 2.1, 2.3, 2.5, 2.1, 2.9, 2.7, 8.4, 2.5, 2.5, 2.9,
>     3, 2.8, 2.5, 2.4, 2.5, 2.4, 2.4, 2.3), TSS = c(9.2, 5.3,
>     NA, NA, NA, 2.8, NA, 1.9, 1.2, 1.3, 5.2, 13, 2, 2.6, 2.1,
>     290, 8.5, 1.3, 7, 2.8, 1.3, 0.9, 1.4, 1.5, 1.6, 7, 1.1),
>     TVSS = c(NA, NA, NA, NA, NA, NA, NA, NA, 2.4, NA, NA, NA,
>     NA, NA, NA, 34, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
>     ), Zinc = c(0.0062, NA, 0.0048, 0.01, NA, 0.0056, 0.0078,
>     0.0029, 0.0033, 0.0035, 0.0062, 0.0061, 0.0038, 0.0039, 0.0062,
>     0.032, 0.0074, 0.0026, 0.0027, NA, 0.0019, NA, NA, 0.0035,
>     0.0041, 0.007, 0.002)), .Names = c("Site",
"Alkalinity",
> "Arsenic", "BOD..5.day", "Cadmium",
"Calcium", "Carbonaceous.BOD..5.day",
> "Chloride", "Chromium", "COD",
"Copper", "Dissolved.Ammonia...N..phenate.",
> "Dissolved.Nitrate.Nitrite...N",
"Dissolved.Nitrite...N", "DOC",
> "Iron", "Lead", "Magnesium",
"Manganese", "Mercury", "Nickel",
> "Ortho.phosphorus", "pH", "Phosphorus",
"Potassium", "Selenium",
> "Silicon", "Sodium", "Sulfate",
"TDS", "TIC", "TKN", "TOC",
"TSS",
> "TVSS", "Zinc"), row.names = 54:80, class =
"data.frame")
>
> library(prettyR)
> library(moments)
> Q25 <- function(x, na.rm=TRUE){quantile(as.numeric(x), 0.25, na.rm =
na.rm,
> names = FALSE)}
> Q75 <- function(x, na.rm=TRUE){quantile(as.numeric(x), 0.75, na.rm =
na.rm,
> names = FALSE)}
> describe(x.f, num.desc=c("mean", "median",
"sd", "min", "max", "skewness",
> "Q25", "Q75","valid.n"))
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894