-
2005-02-27
tapply函数,来自Rhelp[R学习笔记]
tapply package:base R Documentation
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 &aposX&apos.
FUN: the function to be applied. In the case of functions like
'+', '%*%', etc., the function name must be quoted. If &aposFUN'
is &aposNULL', tapply returns a vector which can be used to
subscript the multi-way array &apostapply' normally produces....: optional arguments to &aposFUN&apos.
simplify: If &aposFALSE', &apostapply' always returns an array of mode
'"list"&apos. If &aposTRUE' (the default), then if &aposFUN' always
returns a scalar, &apostapply' returns an array with the mode of
the scalar.Value:
When &aposFUN' is present, &apostapply' calls &aposFUN' for each cell that has
any data in it. If &aposFUN' returns a single atomic value for each
cell (e.g., functions &aposmean' or &aposvar') and when &apossimplify' is
&aposTRUE', &apostapply' returns a multi-way array containing the values.
The array has the same number of dimensions as &aposINDEX' has
components; the number of levels in a dimension is the number of
levels (&aposnlevels()') in the corresponding component of &aposINDEX&apos.Note that contrary to S, &apossimplify = TRUE' always returns an
array, possibly 1-dimensional.If &aposFUN' does not return a single atomic value, &apostapply' returns
an array of mode &aposlist' whose components are the values of the
individual calls to &aposFUN', i.e., the result is a list with a &aposdim'
attribute.Note that optional arguments to &aposFUN' supplied by the &apos...'
argument are not divided into cells. It is therefore
inappropriate for &aposFUN' to expect additional arguments with the
same length as &aposX&apos.References:
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
Language_. Wadsworth & Brooks/Cole.See Also:
the convenience functions &aposby' and &aposaggregate' (using &apostapply');
&aposapply', &aposlapply' with its versions &apossapply' and &aposmapply&apos.Examples:
require(stats)
groups <- as.factor(rbinom(32, n = 5, p = .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, len = 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) -
2005-02-27
R软件概述
R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。
R是S语言的一种实现。S语言是由AT&T贝尔实验室开发的一种用来进行数据探索、统计分析、作图的解释型语言。最初S语言的实现版本主要是S-PLUS。S-PLUS是一个商业软件,它基于S语言,并由MathSoft公司的统计科学部进一步完善。后来Auckland大学的Robert Gentleman 和 Ross Ihaka 及其他志愿人员开发了一个R系统。R的使用与S-PLUS有很多类似之处,两个软件有一定的兼容性。S-PLUS的使用手册,只要经过不多的修改就能成为R的使用手册。所以有人说:R,是S-PLUS的一个"克隆"。但是请不要忘了:R is free。
R是一套完整的数据处理、计算和制图软件系统。其功能包括:数据存储和处理系统;数组运算工具(其向量、矩阵运算方面功能尤其强大);完整连贯的统计分析工具;优秀的统计制图功能;简便而强大的编程语言:可操纵数据的输入和输入,可实现分支、循环,用户可自定义功能。
与其说R是一种统计软件,还不如说R是一种数学计算的环境,因为R并不是仅仅提供若干统计程序、使用者只需指定数据库和若干参数便可进行一个统计分析。R的思想是:它可以提供一些集成的统计工具,但更大量的是它提供各种数学计算、统计计算的函数,从而使使用者能灵活机动的进行数据分析,甚至创造出符合需要的新的统计计算方法。
R是一个免费的自由软件,它有UNIX、LINUX、MacOS和WINDOWS版本,都是可以免费下载和使用的。R的主要网站是http://www.r-project.org。在那儿可以下载到R的安装程序、各种外挂程序和文档。在R的安装程序中只包含了8个基础模块,其他外在模块可以通过CRAN获得(http://cran.r-project.org)。
以下简述R FOR WINDOWS的安装和使用:
在网址:http://cran.r-project.org 下可以找到R的各个版本的安装程序和源代码。点击进入:Windows (95 and later),再点击:base,下载SetupR.exe,约18兆,此便是R FOR WINDOWS的安装程序。双击SetupR.exe,按照提示一步步安装即可。
安装完成后,程序会创建R程序组并在桌面上创建R主程序的快捷方式(也可以在安装过程中选择不要创建)。通过快捷方式运行R,便可调出R的主窗口(如下图 1-1)。
类似于许多以编程方式为主要工作方式的软件,R的界面简单而朴素,只有不多的几个菜单和快捷按钮。快捷按钮下面的窗口便是命令输入窗口,它也是部分运算结果的输出窗口,有些运算结果则会输出在新建的窗口中。
主窗口上方的一些文字是刚运行R时出现的一些说明和指引。文字下的:> 符号便是R的命令提示符,在其后可输出命令;>后的矩形是光标。R一般是采用交互方式工作的,在命令提示符后输入命令,回车后便会输出结果。
在R朴素的界面下,是丰富而复杂的运算功能。在以后的叙述中,我们将一起去探讨R的强大功能。
-
2005-02-27
bootstrap算法3,速度飞快,是前两种的10几倍
rm(list=ls(all=TRUE))
eigenvalue1=numeric(0)
replicates=5000
data1=read.table("bh.txt",header=F) #从文本中读取数据
vectorlength=length(data1$V1) #获取样本数目
vectors=length(data1) #性状数目
newdata=numeric(vectorlength*vectors) #定义临时接受每个随机抽样生成的样本数据
dim(newdata)<-c(vectorlength,vectors)#print(data1)
#print(xx)
#print(data1[1])
macs=numeric(replicates*vectors) #储存所有的replicates个多元异速生长系数的数据
dim(macs)=c(replicates,vectors)for(k in 1:replicates)
{
xx=sample(1:vectorlength,replace=TRUE)
for(i in 1:length(xx))
{
for(j in 1:vectors)
{
newdata[i,j]=data1[xx[i],j]
}}#产生一个群体的随机抽样数据
pc1.cr=eigen(cov(newdata),EISPACK=TRUE)
eigenvectors1=pc1.cr$vectors
for(i in 1:vectors)
{
eigenvalue1[i]=eigenvectors1[i,1]
}
mac1=eigenvalue1/mean(eigenvalue1) #计算多元异速生长系数
#write(mac1,file="macs.txt",ncol=vectors,append=TRUE) # for(i in 1:vectors)
# {
# macs[k,i]=mac1[i]
# }
macs[k,]=mac1;
print(k)
}
write.table(macs,file="macs.txt") #存入文本文档macs.txt
unlink("macs.txt") -
2005-02-27
通过R进行多元异速生长系数的bootstrap模拟
rm(list=ls(all=TRUE))
eigenvalue1=numeric(0)
replicates=5000
data1=read.table("bh.txt",header=F) #从文本中读取数据
vectorlength=length(data1$V1) #获取样本数目
vectors=length(data1) #性状数目
newdata=numeric(vectorlength*vectors) #定义临时接受每个随机抽样生成的样本数据
dim(newdata)<-c(vectorlength,vectors)#print(data1)
#print(xx)
#print(data1[1])
macs=numeric(replicates*vectors) #储存所有的replicates个多元异速生长系数的数据
dim(macs)=c(replicates,vectors)for(k in 1:replicates)
{
xx=sample(1:vectorlength,replace=TRUE)
for(i in 1:length(xx))
{
for(j in 1:vectors)
{
newdata[i,j]=data1[xx[i],j]
}}#产生一个群体的随机抽样数据
pc1.cr=eigen(cov(newdata),EISPACK=TRUE)
eigenvectors1=pc1.cr$vectors
for(i in 1:vectors)
{
eigenvalue1[i]=eigenvectors1[i,1]
}
mac1=eigenvalue1/mean(eigenvalue1) #计算多元异速生长系数
#write(mac1,file="macs.txt",ncol=vectors,append=TRUE) # for(i in 1:vectors)
# {
# macs[k,i]=mac1[i]
# }
macs[k,]=mac1;
print(k)
}
write.table(macs,file="macs.txt") #存入文本文档macs.txt
unlink("macs.txt") -
2005-02-27
统计软件R教程1
R软件是一个自由软件,采用GPL协议:
数据的读取:假设有一批日本对虾的表型形态数据,如下所示:
A B C D E F
1 1.790 1.328 1.418 4.050 3.970 12.20
2 1.472 1.884 2.170 4.300 4.452 14.41
3 1.278 1.558 1.866 3.464 3.638 12.56
4 1.850 1.866 2.052 4.650 4.600 14.32
5 1.214 1.538 1.674 3.570 3.472 12.05
6 1.470 1.954 2.880 4.520 4.360 15.22
7 1.340 1.804 1.980 4.174 4.368 13.80
8 1.470 1.800 2.124 4.340 4.140 14.40
9 1.432 1.438 1.968 4.074 4.296 14.45
10 1.570 2.020 2.200 4.610 4.300 15.51
11 1.720 2.278 2.448 5.310 5.590 17.49
12 1.680 2.228 2.454 4.960 4.320 16.36
13 1.428 1.900 1.962 4.890 4.270 14.02
14 1.490 2.000 2.200 4.630 4.400 15.20
15 1.296 1.606 2.874 3.668 4.120 12.90
16 1.240 1.586 1.854 2.910 3.640 13.50
17 1.536 1.974 2.220 4.394 5.100 15.50
18 1.440 1.942 2.254 4.700 4.660 16.00
19 1.350 1.810 1.928 3.942 4.156 13.70
20 2.160 2.148 2.374 5.050 4.950 16.70
21 1.886 2.276 2.400 5.430 5.542 18.30
22 1.470 2.084 2.200 4.310 4.680 15.30
23 1.682 2.128 2.390 5.200 5.668 16.70
24 1.690 2.170 2.444 4.740 4.710 16.40
25 1.500 1.990 2.330 4.706 5.600 15.30
26 1.836 2.344 2.250 5.640 6.000 18.30
27 1.320 1.630 1.890 3.756 4.250 13.00
28 1.120 1.560 1.674 3.400 3.220 12.60
存为文本文件bh.dat, 假设在D:\
读取这个文本文件的命令为
>bhdata = read.table(file="D:/bh.dat',header=T)
注意路径中的"/",而不是通常windows下的"\"。
然后敲入bhdata可以查看数据
>bhdata
如果求这六个性状平均值
>bhmean <- mean(bhdata)
>bhmean
>1.526071 1.887286 2.159929 4.406714 4.516857 14.863929
>bhsd <- sd(bhdata)
>bhsd
>0.2375389 0.2747558 0.3299856 0.6600135 0.6910684 1.7565672 -
2005-02-25
正式开始通过outlook和Zoundry进行blog发布
呵呵,用outlook习惯了,即便是写blog,也不想上网了。可是用了几个桌面端的blog
发布工具,感觉要不是很复杂,就是功能不行。今天发现msn竟然支持电子邮件发布,
所以重新启用了msn空间,现在的blog有好几个了,可惜没有一个能够坚持到底。可能
个人的本性就如此吧! -
2005-02-25
如何查询手机的序列号
所有的手机都有一个唯一的序列号,当你键入*#06# 时,你手机的屏幕上就会出现一个 15位的数字,这个数字就是你的手机的序列号。请把此号码记下来并保存好,当你的手 机被盗后,你可以将此号码提供给你的服务商,他能将你丢失的手机锁住,即使别人将 你原有的SIM卡换掉,此手机也不能使用。 也许你不会失而复得,但是你的手机对他们来讲也是没有任何价值,除非将它作为纸镇。 如果我们每个人都能够知道自己手机的唯一的序列号,那么盗窃手机将变得没有任何意义。请将这个信息转告尽可能多的人,这个常识普及了,大家丢手机的机会就少很多。
-
2005-02-25
python的计算器用法
在控制台下,最后一次代数运算的结果,保存在"_"中,这样可以方便计算。







