分组功能(tapply,by,aggregate)和* apply系列

问题:

Whenever I want to do something "map"py in R, I usually try to use a function in the apply family. 每当我想在R中作“ map” py任务时,我一般都会尝试在apply系列中使用一个函数。 数组

However, I've never quite understood the differences between them -- how { sapply , lapply , etc.} apply the function to the input/grouped input, what the output will look like, or even what the input can be -- so I often just go through them all until I get what I want. 可是,我从未彻底理解它们之间的区别-{ sapplylapply等}如何将函数应用于输入/分组输入,输出将是什么样,甚至输入是什么-因此我常常只是遍历全部这些,直到获得想要的东西。 app

Can someone explain how to use which one when? 谁能解释何时使用哪个? ide

My current (probably incorrect/incomplete) understanding is... 我目前(可能不正确/不完整)的理解是... 函数

  1. sapply(vec, f) : input is a vector. sapply(vec, f) :输入是向量。 output is a vector/matrix, where element i is f(vec[i]) , giving you a matrix if f has a multi-element output 输出是一个向量/矩阵,其中元素if(vec[i]) ,若是f具备多元素输出,则为您提供矩阵 ui

  2. lapply(vec, f) : same as sapply , but output is a list? lapply(vec, f) :与sapply相同,可是输出是一个列表? spa

  3. apply(matrix, 1/2, f) : input is a matrix. apply(matrix, 1/2, f) :输入是一个矩阵。 output is a vector, where element i is f(row/col i of the matrix) 输出是一个向量,其中元素i为f(矩阵的行/列i)
  4. tapply(vector, grouping, f) : output is a matrix/array, where an element in the matrix/array is the value of f at a grouping g of the vector, and g gets pushed to the row/col names tapply(vector, grouping, f) :输出是一个矩阵/数组,其中矩阵/数组中的元素是向量分组g处的f值,而且g被推到行/列名
  5. by(dataframe, grouping, f) : let g be a grouping. by(dataframe, grouping, f) :令g为一个分组。 apply f to each column of the group/dataframe. f应用于组/数据框的每一列。 pretty print the grouping and the value of f at each column. 在每列漂亮地打印分组和f的值。
  6. aggregate(matrix, grouping, f) : similar to by , but instead of pretty printing the output, aggregate sticks everything into a dataframe. aggregate(matrix, grouping, f) :相似于by ,可是aggregate不会将输出漂亮地打印by ,而是将全部内容粘贴到数据框中。

Side question: I still haven't learned plyr or reshape -- would plyr or reshape replace all of these entirely? 侧问题:我尚未学会plyr或重塑-将plyrreshape取代全部这些彻底? .net


解决方案:

参考一: https://stackoom.com/question/EhzZ/分组功能-tapply-by-aggregate-和-apply系列
参考二: https://oldbug.net/q/EhzZ/Grouping-functions-tapply-by-aggregate-and-the-apply-family
相关文章
相关标签/搜索