swirl 10: lapply and sapply
来源:互联网 发布:c语言中取反 编辑:程序博客网 时间:2024/06/05 06:18
These powerful functions, along with their close relatives (vapply() and tapply(), among others) offer a concise and convenient means of implementing theSplit-Apply-Combine strategyfor data analysis.
Throughout this lesson, we'll use the Flags dataset from the UCI Machine Learning Repository. This dataset contains details of various nations and their flags. More information may be found here: http://archive.ics.uci.edu/ml/datasets/Flags
viewinfo() To open a more complete description of the dataset in a separate text file, type viewinfo()
lapply() The lapply() function takes a list as input, applies a function to each element of the list, then returns a list of the same length as the original one.
Type cls_list <- lapply(flags, class) to apply the class() function to each column of the flags dataset and store the result in a variable called cls_list. Note that you just supply the name of the function you want to apply (i.e. class), without the usual parentheses after it.
The 'l' in 'lapply' stands for 'list'. Type class(cls_list) to confirm that lapply() returned a list.
as.character() In this case, since every element of the list returned by lapply() is a character vector of length one (i.e. "integer" and "vector"), cls_list can be simplified to a character vector. To do this manually, type as.character(cls_list).
sapply() allows you to automate this process by calling lapply() behind the scenes, but then attempting to simplify (hence the 's' in 'sapply') the result for you. In general, if the result is a list where every element is of length one, then sapply() returns a vector. If the result is a list where every element is a vector of the same length (> 1), sapply() returns a matrix. If sapply() can't figure things out, then it just returns a list, no different from what lapply() would give you.
unique() When given a vector, the unique() function returns a vector with all duplicate elements removed. In other words, unique() returns a vector of only the 'unique' elements.
function(elem) elem[2] lapply(unique_vals, function(elem) elem[2]) will return a list containing the second item from each element of the unique_vals list. Note that our function takes one argument, elem, which is just a 'dummy variable' that takes on the value of each element of unique_vals, in turn.
anonymous functions The only difference between previous examples and this one is that we are defining and using our own function right in the call to lapply(). Our function has no name and disappears as soon as lapply() is done using it. So-called 'anonymous functions' can be very useful when one of R's built-in functions isn't an option.
- swirl 10: lapply and sapply
- R Learning-swirl-lapply and sapply
- learning R with lapply and sapply(不懂)
- lapply &sapply note
- apply lapply sapply等R函数
- R apply、tapply、lapply、sapply、mapply 说明
- R apply, lapply, sapply, mapply, tapply functions
- R中的apply、lapply和sapply
- Usingle apply, sapply, lapply in R
- apply, tapply, sapply, lapply, mapply, table等用法
- R apply、tapply、lapply、sapply、mapply、table等函数
- swirl 2: Workspace and Files
- [Getting and Cleaning data] swirl
- R中利用apply、tapply、lapply、sapply、mapply、table等函数进行分组统计
- R中的apply、tapply、lapply、sapply、mapply、table等函数介绍
- R中利用apply、tapply、lapply、sapply、mapply、table等函数进行分组统计
- sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate
- R中的高效批量处理函数(lapply sapply apply tapply mapply)
- 面向对象和面向组件的区别
- PHP 类中的静态变量的继承
- hdu1870愚人节的礼物之天呐你们为什么要骗我用栈才能过。。。大水题。。。
- java中Date与String的相互转化
- IT项目管理(原书第7版)——互动出版网
- swirl 10: lapply and sapply
- 闪电特效算法
- poj解题报告——2425
- 如何使用Cocos2d-x 3.0制作基于tilemap的游戏:第二部分
- NSDictionary、NSMutableDictionary的基本用法
- Spark Graphx 构建graph和aggregateMessages聚合消息
- 乐点 Android SDK 积分墙开发者文档
- Fixing the Great Wall
- HDU - 2553 N皇后问题