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. 可是,我从未彻底理解它们之间的区别-{ sapply
, lapply
等}如何将函数应用于输入/分组输入,输出将是什么样,甚至输入是什么-因此我常常只是遍历全部这些,直到获得想要的东西。 app
Can someone explain how to use which one when? 谁能解释何时使用哪个? ide
My current (probably incorrect/incomplete) understanding is... 我目前(可能不正确/不完整)的理解是... 函数
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 输出是一个向量/矩阵,其中元素i
为f(vec[i])
,若是f
具备多元素输出,则为您提供矩阵 ui
lapply(vec, f)
: same as sapply
, but output is a list? lapply(vec, f)
:与sapply
相同,可是输出是一个列表? spa
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) 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
被推到行/列名 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
的值。 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或重塑-将plyr
或reshape
取代全部这些彻底? .net