r软件及统计分析因子列表数据框程序设计结构(编辑修改稿)内容摘要:

出函数利用 cat()和 formatC来控制: apply(da, 1, function(r) cat(formatC(r[1], format=39。 d39。 , width=2, flag=39。 039。 ), 39。 39。 , formatC(r[2], format=39。 d39。 , width=2, flag=39。 039。 ), 39。 39。 , formatC(r[3], format=39。 d39。 , width=2, flag=39。 039。 ), 39。 \n39。 , sep=39。 39。 )) 990103 961109 650518 NULL • 这里我们知道 apply函数第一个参数指定了一个矩阵,第二个参数说明对行操作还是对列操作,第三个参数是一个函数,这里我们使用了直接定义一个函数作为参数的办法。 输出结果中多了一个 NULL函数,这是因为我们在交互运行, apply的结果作为一个表达式的值( NULL )会被显示出来。 • 为避免显示,可以把结果赋给一个临时变量名,或者把整个表达式作为 invisible() 函数的参数,这时不显示表达式值。 • S的输出缺省显示在交互窗口。 可以用 sink()函数指定一个文件以把后续的输出转向到这个文件,并可用 append参数指定是否要在文件末尾附加: sink(“E:/work/, append=TRUE) ls() d sink() 调用无参数的 sink()把输出恢复到交互窗口。 • write(t(x),file=„文件名’, nol=nol(x))把一个矩阵 X输出到文件中,把 X转置后输出因为 R中矩阵是列优先的,如果不转置则输出是按列输出的。 如果不指定列数则缺省使用 5列。 文件名缺省用’ data‟. • (x,file=„文件名’ )把数据框 X输出到文件中,输出包括变量名表头和行名。 ’ . • cl = (“E:/R/, =c(1), + =c(Name, Sex, Age, Height, Weight)) cl 输入 • 为了从外部文件读入一个数值型向量, S提供了 scan()函数。 如果指定了 file参数(也是第一参数),则从指定文件读入,缺省情况下读入一个数值向量,文件中各数据以空白分隔,读到文件尾为止。 例如: • cat(1:12, 39。 \n39。 , file=„E:/work/39。 ) • x scan(39。 c:/work/39。 ) • 如果文件中是一个用 空白分隔 的矩阵(或数组),我们可以先用 scan()把它读入到一个向量然后用 matrix()函数(或 array()函数)转换。 如: • y matrix(scan(„E:/work/39。 ), ncol=3, byrow=T) • 实际上, scan()也能够读入一个多列的表格,只要用what参数指定一个列表,则列表每项的类型为需要读取的类型。 用 skip参数可以跳过文件的开始若干行不读。 用 sep参数可以指定数据间的分隔符。 详见帮助。 • scan()不指定读取文件名时是交互读入,读入时用一个空行结束。 • 如果要读取一个数据框, S提供了一个 ()函数。 它只要给出一个文件名,就可以把文件中用空白分隔的表格数据每行读入为数据框的一行。 比如,文件E:\ 中内容如下: Zhou 15 3 “Li Ming” 9 李明 Zhang Wang • 用 : x („E:/39。 , =T) x V1 V2 V3 1 Zhou 15 3 2 Li Ming 9 李明 3 Zhang Wang 读入结果为数据框。 • 函数可以自动识别表列是数值型还是字符型,并在缺省情况下把字符型数据转换为因子(加上 =T可以保留字符型不转换)。 • 函数自动为数据框变量指定“ V1 ”、“ V2”这样的变量名,指定“ 1”、“ 2”这样的行名。 可以用 指定一个字符型向量作为数据框的变量名。 • ()可以读入带有表头的文件,只要加上header=TRUE参数即可。 可以用 sep 参数指定表行各项的分隔符。 例如,为了读入如下带有表头的逗号分隔文件 E:\: Name,score, Zhou,15,3 Li Ming, 9, 李明 Zhang, , Wang • 使用如下语句: x=(„E:/39。 , header=T, sep=39。 ,39。 ) x Name score 1 Zhou 3 2 Li Ming 李明 3 Zhang Wang • 其它一些用法见帮助。 • cl =(E:/R/, =c(1), =c(Name, Sex, Age, Height, Weight))。 cat(39。 names(cl)39。 ,names(cl))。 • =(E:/R/,header=T,sep=39。 ,39。 )。 cat(39。 names()39。 ,names())。 • Edata = (39。 E:/R/Employee 39。 ,header=T,sep=39。 ,39。 ) cat(39。 names(Edata)39。 ,names(Edata))。 • package:foreign R Documentation Read an SPSS data file • Description: 39。 39。 reads a file stored by the SPSS 39。 save39。 and 39。 export39。 mands and returns a list. • Usage: (file, =TRUE, =FALSE, =Inf, =FALSE) • Arguments: file: character variable with the name of the file to read. : Convert variables with value labels into R factors with those levels? : return a data frame? : Only variables with at most this many unique values will be converted to factors : Trim trailing spaces from factor levels? • Details: This uses modified code from the PSPP project for reading the SPSS formats. Occasionally in SPSS value labels will be added to some values of a continuous variable (eg to distinguish different types of missing data), and you will not want these variables converted to factors. By setting 39。 39。 you can specify that variables with a large number of distinct values are not converted to factors even if they have value labels. In addition, variables will not be converted to factors if there are nonmissing values that have no value label. The value labels are then returned in the 39。 39。 attribute of the variable. • If SPSS variable labels are present, they are returned as the 39。 39。 attribute of the answer. Fixed length strings (including value labels) are padded on the right with spaces by SPSS, and so are read that way by R. See the examples for 39。 sub39。 for ways to remove trailing spaces in string data. • Value: A list (or data frame) with one ponent for each variable in the saved data set. • Note: If SPSS value labels are converted to factors the underlying numerical codes will not in general be the same as the SPSS numerical values, since the numerical codes in R are always 1,2,3,... • Author(s): Saikat DebRoy • Examples: Not run: (datafile) don39。 t convert value labels to factor levels (datafile,=FALSE) convert value labels。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。