{"id":334,"date":"2013-11-22T23:48:40","date_gmt":"2013-11-23T04:48:40","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=334"},"modified":"2013-11-22T23:48:40","modified_gmt":"2013-11-23T04:48:40","slug":"r-apply-family","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2013\/11\/22\/r-apply-family\/","title":{"rendered":"R Apply Family"},"content":{"rendered":"<div>\n<div>\n<pre># Apply Functions Over Array Margins\napply; sweep; scale\n\n# Apply a Function over a List or Vector\nlapply; sapply; vapply; replicate; rapply\n\n# Apply a Function to Multiple List or Vector Arguments\nmapply; Vectorize\n\n# Apply a Function Over a Ragged Array\ntapply\n\n# Apply a Function to a Data Frame Split by Factors\nby; aggregate; split\n\n# Apply a Function Over Values in an Environment\neapply<\/pre>\n<\/div>\n<\/div>\n<div><a name=\"apply\"><\/a><a name=\"sweep\"><\/a><a name=\"scale\"><\/a><\/p>\n<div>apply sweep scale<\/div>\n<div>\n<pre># \"apply\" returns a vector or array or list of values obtained by applying\n#   a function to margins of an array or matrix.\n(m = matrix(1:6, nrow = 2))\napply(m, 1, sum)\napply(m, 1:2, sqrt)\n\n# \"sweep\" returns an array obtained from an input array by sweeping out\n#   a summary statistic.\n(X = array(1:24, dim = 4:2))\nsweep(X, 1, apply(X, 1, mean))\n\n# \"scale\" is generic function whose default method centers and\/or scales\n#   the columns of a numeric matrix.\n(mat = matrix(round(rnorm(10)*10), nrow = 5))\nscale(mat)\napply(mat, 2, mean)\napply(mat, 2, sd)\ncov(scale(mat))<\/pre>\n<\/div>\n<\/div>\n<div><a name=\"lapply\"><\/a><a name=\"sapply\"><\/a><a name=\"vapply\"><\/a><a name=\"replicate\"><\/a><a name=\"rapply\"><\/a><\/p>\n<div>lapply sapply vapply replicate rapply<\/div>\n<div>\n<pre># \"lapply\" returns a list of the same length as X, each element of which is\n#   the result of applying FUN to the corresponding element of X.\nlapply(split(Orange[2:3], Orange[1]), mean)\nlapply(1:5, sqrt)\n\n# \"sapply\" is a user-friendly version of lapply by default returning\n#   a vector or matrix if appropriate.\nsapply(split(Orange[2:3], Orange[1]), mean)\nsapply(1:5, sqrt)\n\n# \"vapply\" is similar to sapply, but has a pre-specified type of return value,\n#   so it can be safer (and sometimes faster) to use.\nvapply(1:5, sqrt, 1i)\n\n# \"replicate\" is a wrapper for the common use of sapply for repeated evaluation\n#   of an expression (which will usually involve random number generation).\nreplicate(3, rnorm(5))\n\n# \"rapply\" is a recursive version of lapply.\n(x = list(A = list(a=pi, b=list(b1=1)), B = \"a character string\"))\nrapply(x, sqrt) # Error\nrapply(x, sqrt, classes = \"numeric\", how = \"unlist\")\nrapply(x, sqrt, classes = \"numeric\", how = \"replace\")\nrapply(x, sqrt, classes = \"numeric\", how = \"list\")\nrapply(x, sqrt, classes = \"numeric\", deflt = NA, how = \"unlist\")\nrapply(x, sqrt, classes = \"numeric\", deflt = NA, how = \"replace\")\nrapply(x, sqrt, classes = \"numeric\", deflt = NA, how = \"list\")\nrapply(x, round, classes = \"numeric\", how = \"replace\", digits = 2)<\/pre>\n<\/div>\n<\/div>\n<div><a name=\"mapply\"><\/a><a name=\"Vectorize\"><\/a><\/p>\n<div>mapply Vectorize<\/div>\n<div>\n<pre># \"mapply\" is a multivariate version of sapply. mapply applies FUN to\n#   the first elements of each ... argument, the second elements,\n#   the third elements, and so on. Arguments are recycled if necessary.\nmapply(rep, LETTERS[1:3], 1:3)\n\n# \"Vectorize\" returns a new function that acts as if mapply was called.\nvrep = Vectorize(rep)\nvrep(LETTERS[1:3], 1:3)\nvrep = Vectorize(rep.int)\nvrep(LETTERS[1:3], 1:3)<\/pre>\n<\/div>\n<\/div>\n<div><a name=\"tapply\"><\/a><\/p>\n<div>tapply<\/div>\n<div>\n<pre># \"tapply\" applies a function to each cell of a ragged array, that is\n#   to each (non-empty) group of values given by a unique combination of\n#   the levels of certain factors.\n(m = matrix(1:6, 2, 3))\n(fac = matrix(c(1,3,1,2,2,2), 2, 3))\ntapply(m, fac, sum)<\/pre>\n<\/div>\n<\/div>\n<div><a name=\"by\"><\/a><a name=\"aggregate\"><\/a><a name=\"split\"><\/a><\/p>\n<div>by aggregate split<\/div>\n<div>\n<pre># \"by\" is an object-oriented wrapper for tapply applied to data frames.\nby(Orange[2:3], Orange[1], mean)\ndo.call(\"rbind\", by(Orange[2:3], Orange[1], mean))\n\n# \"aggregate\" splits the data into subsets, computes summary statistics\n#   for each, and returns the result in a convenient form.\naggregate(Orange[2:3], Orange[1], mean)\n# See help for usage of NA, formula, dot notation, xtabs, nfrequency.\n\n# \"split\" divides the data in the vector x into the groups defined by f.\n#   The replacement forms replace values corresponding to such a division.\nsplit(Orange[2:3], Orange[1])<\/pre>\n<\/div>\n<\/div>\n<div><a name=\"eapply\"><\/a><\/p>\n<div>eapply<\/div>\n<div>\n<pre># \"eapply\" applies FUN to the named values from an environment and returns\n#   the results as a list. The user can request that all named objects\n#   are used (normally names that begin with a dot are not). The output\n#   is not sorted and no parent environments are searched.\nenv = new.env()\nenv$x = 1:3\nenv$y = 4:6\neapply(env, sum)<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p># Apply Functions Over Array Margins apply; sweep; scale # Apply a Function over a List or Vector lapply; sapply; vapply; replicate; rapply # Apply&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}