Hi, What was I doing wrong, such that B) gave warning messages ? I want the computation of thr to be outside the apply function.
A) Uses a simple matrix of 9 elements. No warning messages. data2_1 <- matrix (c(1,2,3,NA,4,5,NA,NA,6), 3,3) mean <- colMeans(data2_1, na.rm = TRUE) sd <- sd(data2_1, na.rm = TRUE) for (i in 1:2) { thr <- mean + i*sd num <- apply(data2_1, 2, function(x) { temp <- thr sum(x > (temp), na.rm = TRUE)}) } B) Replace data2_1 with a larger matrix, gives 20 warning messages i.e., In x > (temp) : = longer object length is not a multiple of shorter object length data2_1 <- matrix(c(0.9584190, 0.2710325, -0.9495618, -0.1301772, -0.7539687, 0.5344464, -0.8205933, 0.1581723, -0.5351588, 0.04448065, 0.9936430, 0.2278786, -0.8160700, -0.3314779, -0.4047975, 0.1168152, -0.7458182, - 0.2231588, -0.5051651, -0.74871174, 0.9450363, 0.4797723, -0.9033313, - 0.5825065, 0.8523742, 0.7402795, -0.7134312, -0.8162558, 0.6345438, - 0.05704138), 3,10) mean <- colMeans(data2_1, na.rm = TRUE) sd <- sd(data2_1, na.rm = TRUE) for (i in 1:2) { thr <- mean + i*sd num <- apply(data2_1, 2, function(x) { temp <- thr sum(x > (temp), na.rm = TRUE)}) } C) To remove the 20 warning messages, I have to replace temp <- thr with temp <- mean + i*sd data2_1 <- matrix(c(0.9584190, 0.2710325, -0.9495618, -0.1301772, -0.7539687, 0.5344464, -0.8205933, 0.1581723, -0.5351588, 0.04448065, 0.9936430, 0.2278786, -0.8160700, -0.3314779, -0.4047975, 0.1168152, -0.7458182, - 0.2231588, -0.5051651, -0.74871174, 0.9450363, 0.4797723, -0.9033313, - 0.5825065, 0.8523742, 0.7402795, -0.7134312, -0.8162558, 0.6345438, - 0.05704138), 3,10) mean <- colMeans(data2_1, na.rm = TRUE) sd <- sd(data2_1, na.rm = TRUE) for (i in 1:2) { thr <- mean + i*sd num <- apply(data2_1, 2, function(x) { temp <- mean + i*sd sum(x > (temp), na.rm = TRUE)}) } [[alternative HTML version deleted]] ______________________________________________ R-help@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.