table可统计数据的频数

tapply可根据因子、向量和要计算的函数计算

> class<-c(1,2,3,2,1,2,1,3)

> class
[1] 1 2 3

> c(81,65,72,88,73,91,56,90)->student
> class
[1] 1 2 3 2 1 2 1 3

>factor(class)->class

> tapply(student,class,mean)
       1        2        3
70.00000 81.33333 81.00000
> tapply(student,class,min)
 1  2  3
56 65 72

> tapply(student,class,max)
 1  2  3
81 91 90
> table(class)
class
1 2 3
3 3 2
>

Apply a Function Over a Ragged Array

Description

Apply a function to each cell of a ragged array, that is to each (non-empty) group of values given by a unique combination of the levels of certain factors.

Usage

tapply(X, INDEX, FUN = NULL, ..., simplify = TRUE)

Arguments

X

an atomic object, typically a vector.

INDEX

list of factors, each of same length as X. The elements are coerced to factors by as.factor.

FUN

the function to be applied, or NULL. In the case of functions like +%*%, etc., the function name must be backquoted or quoted. If FUN is NULL, tapply returns a vector which can be used to subscript the multi-way array tapply normally produces.

...

optional arguments to FUN: the Note section.

simplify

If FALSEtapply always returns an array of mode "list". If TRUE (the default), then if FUN always returns a scalar, tapply returns an array with the mode of the scalar.

Value

If FUN is not NULL, it is passed to match.fun, and hence it can be a function or a symbol or character string naming a function.

When FUN is present, tapply calls FUN for each cell that has any data in it. If FUN returns a single atomic value for each such cell (e.g., functions mean or var) and when simplify is TRUE,tapply returns a multi-way array containing the values, and NA for the empty cells. The array has the same number of dimensions as INDEX has components; the number of levels in a dimension is the number of levels (nlevels()) in the corresponding component of INDEX. Note that if the return value has a class (e.g. an object of class "Date") the class is discarded.

Note that contrary to S, simplify = TRUE always returns an array, possibly 1-dimensional.

If FUN does not return a single atomic value, tapply returns an array of mode list whose components are the values of the individual calls to FUN, i.e., the result is a list with a dimattribute.

When there is an array answer, its dimnames are named by the names of INDEX and are based on the levels of the grouping factors (possibly after coercion).

For a list result, the elements corresponding to empty cells are NULL.

Note

Optional arguments to FUN supplied by the ... argument are not divided into cells. It is therefore inappropriate for FUN to expect additional arguments with the same length as X.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

the convenience functions by and aggregate (using tapply); applylapply with its versions sapplyand mapply.

Examples

require(stats) groups <- as.factor(rbinom(32, n = 5, prob = 0.4)) tapply(groups, groups, length) #- is almost the same as table(groups) ## contingency table from data.frame : array with named dimnames tapply(warpbreaks$breaks, warpbreaks[,-1], sum) tapply(warpbreaks$breaks, warpbreaks[, 3, drop = FALSE], sum) n <- 17; fac <- factor(rep(1:3, length = n), levels = 1:5) table(fac) tapply(1:n, fac, sum) tapply(1:n, fac, sum, simplify = FALSE) tapply(1:n, fac, range) tapply(1:n, fac, quantile) ## example of ... argument: find quarterly means tapply(presidents, cycle(presidents), mean, na.rm = TRUE) ind <- list(c(1, 2, 2), c("A", "A", "B")) table(ind) tapply(1:3, ind) #-> the split vector tapply(1:3, ind, sum)


问题:

有数万个数据,两列数据 一列为名称(A列 ) 一列为值(x列),一个名称可对应多个值,一个值可能有多个名称,具体问题如下所示
A1 x1
A2 x2
A3 x3
A4 x4
A1 x5
A2 x3
A5 x6
A1 x7

想得到的结果,将A列名称唯一化,出现一个值对应多个值的列表,且想批量处理
A1 x1 x5 x7
A2 x2 x3
A3 x3
A4 x4
A5 x6

解决方案1:perl

use strict;
use warnings;

my %hash;
open OUT, "> lines.txt" or die"$!";

while () {
chomp;
my ($line1,$line2) = split/\s+/;
push @{$hash{$line1}},$line2;
}

foreach my $key(sort keys %hash) {
print OUT "$key\t@{$hash{$key}}\n";
}

close OUT;
__DATA__
A1 x1
A2 x2
A3 x3
A4 x4
A1 x5
A2 x3
A5 x6

A1 x7 
 
解决方案2:R
d = read.table("data.txt")
tapply(d[,2], d[,1], print)

R-table和tapply函数的更多相关文章

  1. R语言中apply函数

    前言 刚开始接触R语言时,会听到各种的R语言使用技巧,其中最重要的一条就是不要用循环,效率特别低,要用向量计算代替循环计算. 那么,这是为什么呢?原因在于R的循环操作for和while,都是基于R语言 ...

  2. C调用lua的table里面的函数

    网上搜索C.C++调用lua函数,有一大堆复制粘贴的. 但是搜索<C调用lua的table里面的函数> 怎么就没几个呢? 经过探索,发现其实逻辑是这样的: 1.根据name获取table ...

  3. R语言数据读入函数read.table

    1.read.table:可以读TXT也可以读CSV (1)file:文件名 (2)header:是否包含表头 (3)sep:分隔符,如果不设定默认是空格 (4)dec:标志小数点符号,有些国家的小数 ...

  4. R apply() 函数和 tapply() 函数

    apply(a,b,c) a是矩阵 b是行或列的代表,1是行,2是列 c是执行函数,如求和-sum,求平均-mean,求-range tapply(a,b,c) a是一个一维数据,           ...

  5. R语言:常用函数【转】

    数据结构 一.数据管理vector:向量 numeric:数值型向量 logical:逻辑型向量 character:字符型向量list:列表 data.frame:数据框 c:连接为向量或列表len ...

  6. R语言——基本绘图函数

    通过一个综合的例子测试绘图函数 学习的内容是tigerfish老师的教程. 第一节:基本知识 用seq函数产生100位学生的学号. > num = seq(,) > num [] [] [ ...

  7. R8—批量生成文件夹,批量读取文件夹名称+R文件管理系统操作函数

    一. 批量生成文件夹,批量读取文件夹名称 今日,工作中遇到这样一个问题:boss给我们提供了200多家公司的ID代码(如6007.7920等),需要根据这些ID号去搜索下载新闻,从而将下载到的新闻存到 ...

  8. R语言常用数学函数

    语言的数学运算和一些简单的函数整理如下: 向量可以进行那些常规的算术运算,不同长度的向量可以相加,这种情况下最短的向量将被循环使用.   > x <- 1:4 > a <- 1 ...

  9. R语言 三个函数sort();rank();order()

    R语言入门,弄懂了几个简单的函数,分享一下:R语言排序有几个基本函数: sort():rank():order()sort()是对向量进行从小到大的排序rank()返回的是对向量中每个数值对应的秩or ...

随机推荐

  1. java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException 的解决办法之一

    在查看别人的代码的时候,遇到了把工程导入到 Android Studio ,报:ExecutionException: com.android.ide.common.process.ProcessEx ...

  2. CentOS与RedHat的关系

    一.CentOS 与 RedHat 的关系 RedHat 在发行的时候,有两种方式:二进制的发行方式以及源代码的发行方式.无论是哪一种发行方式,你都可以免费获得(例如从网上下载),并再次发布.但如果你 ...

  3. 安装MySQL start Service(无法启动服务)

    在xp是这样:  C:\ProgramData\MySQL  在win7 或者win8 在C:\ProgramData\MySQL   这里还有MySQL的文件,必须要删除    注意:Applica ...

  4. .NET CORE2.0发布后没有 VIEWS视图页面文件

    以前做的CORE1.0的项目,发布的时候有views文件夹的,升级VS后用CORE2.0做项目,发布后没有views文件夹了,全编译到一个类似于Niunan.ZYYCY.Web.Precompiled ...

  5. Atitit 切入一个领域的方法总结 attilax这里,机器学习为例子

    Atitit 切入一个领域的方法总结 attilax这里,机器学习为例子 1.1. 何为机器学习?1 1.2. 两类机器学习算法 :监督式学习(Supervised Learning)和非监督式学习( ...

  6. 浅析C#中的托付

    托付是寻址方法的.NET版本号.在C++中.函数指针仅仅只是是一个指向内存位置的指针,它不是类型安全的.而.NET托付全然不同,托付是安全类型的类,它定义了返回类型和參数的类型. 当把方法传递给其它方 ...

  7. close Spark Streaming gratefully

    https://blog.csdn.net/u010454030/article/details/78679930 https://blog.csdn.net/u010454030/article/d ...

  8. 源码分析HotSpot GC过程(三):TenuredGeneration的GC过程

    老年代TenuredGeneration所使用的垃圾回收算法是标记-压缩-清理算法.在回收阶段,将标记对象越过堆的空闲区移动到堆的另一端,所有被移动的对象的引用也会被更新指向新的位置.看起来像是把杂陈 ...

  9. django中celery的使用

    1.什么是celery celery是一个异步任务框架,当我们的程序中存在一个比较耗时的操作时,可以启动这个异步任务框架, 将耗时操作,交给它来完成,这样节省了程序的执行时间. 2.celery的原理 ...

  10. Android 编程下通过 Theme 和 Style 避免 APP 启动闪黑屏

    之前在做 APP 的时候不太关注这个问题,因为自己在使用其他 APP 的时候也会在应用启动的初始有一个黑屏闪过后才会出现应用的欢迎页.直到最近开发过程中发现自己在欢迎页启动的线程由于请求和处理的数据量 ...