|
30秒注册登陆,可查看更多信息,结交更多好友,享用更多功能,轻松玩转论坛,白白手拉手欢迎您的加入!
您需要 登录 才可以下载或查看,没有账号?注册新成员
x
[size=11.000000pt]When you have spent a lot of time getting a data frame into the desired form, youshouldn't need to recreate your work each time you restart your R session. To save a[size=11.000000pt]data structure to a le that can be reloaded later or transferred to another system, use[size=11.000000pt]the [size=10.000000pt]save() [size=11.000000pt]function. The [size=10.000000pt]save() [size=11.000000pt]function writes one or more R data structures to the[size=11.000000pt]location speci ed by the [size=10.000000pt]file [size=11.000000pt]parameter. R data les have an [size=10.000000pt].RData [size=11.000000pt]extension.(使用sava函数保存数据结构到本地。指定一个文件名参数,文件名以.RData为扩展名)
Suppose you have three objects named x, y, and z that you would like to save in apermanent le. Regardless of whether they are vectors, factors, lists, or data frames,we could save them to a le named mydata.RData using the following command保存x, y,z三个对象到本地,如图1)[size=11.000000pt]
[size=11.000000pt]The [size=10.000000pt]load() [size=11.000000pt]command can recreate any data structures that have been saved toan [size=10.000000pt].RData [size=11.000000pt] le. To load the [size=10.000000pt]mydata.RData [size=11.000000pt] le we saved in the preceding code,[size=11.000000pt]simply typeload函数将重新创建保存在RData文件中的数据结构。见图2)
[size=11.000000pt]If you need to wrap up your R session in a hurry, the [size=10.000000pt]save.image() [size=11.000000pt]command will[size=11.000000pt]write your entire session to a le simply called [size=10.000000pt].RData[size=11.000000pt]. By default, R will look for[size=11.000000pt]this le the next time you start R, and your session will be recreated just as you had[size=11.000000pt]left it.(使用sava.image()函数保存当前会话到本地文件中,下次R启动时就会自动寻找这个文件。你保存在其中的会话就会重新创建)
After working on an R session for sometime, you may have accumulated a numberof data structures. The ls() listing function returns a vector of all the data structurescurrently in the memory. For example, if you've been following along with the codein this chapter, the ls() function returns the following使用ls函数列出内存中存在的所有对象)[size=11.000000pt]
[size=11.000000pt]R will automatically remove these from its memory upon quitting the session, but forlarge data structures, you may want to free up the memory sooner. The [size=10.000000pt]rm() [size=11.000000pt]removefunction can be used for this purpose. For example, to eliminate the [size=10.000000pt]m [size=11.000000pt]and [size=10.000000pt]subject1[size=11.000000pt]objects, simply typeR还在会话结束时会自动清除内存中相关的对象。对于一些非常大的对象,就需要手动的及时释放,可以使用rm函数释放。)
[size=18.000000pt]Importing and saving data from CSV files(导入csv文件和保存数据到csv文件)
[size=11.000000pt]Given a patient data le named [size=10.000000pt]pt_data.csv [size=11.000000pt]located in the R working directory, the[size=10.000000pt]read.csv() [size=11.000000pt]function can be used as follows to load the le into R使用read.csv函数读取csv文件,见图3)
[size=11.000000pt]This will read the CSV le into a data frame titled [size=10.000000pt]pt_data[size=11.000000pt]. Just as we did previouslywhile c**tructing a data frame, we need to use the [size=10.000000pt]stringsAsFactors = FALSE[size=11.000000pt]parameter to prevent R from converting all text variables into factors. This step isbetter left to you, not R, to perform.(注意图3中 read.csv函数中参数[size=13.3333px]stringsAsFactors = FALSE,这个是防止R自动将文本数据转换成因子数据结构)
[size=11.000000pt]By default, R [size=11.000000pt]assumes that the CSV le includes a header line listing the names ofthe features in the dataset. If a CSV le does not have a header, specify the option[size=10.000000pt]header = FALSE[size=11.000000pt], as shown in the following command, and R will assign defaultfeature names in the [size=10.000000pt]V1 [size=11.000000pt]and [size=10.000000pt]V2 [size=11.000000pt]forms and so on:(默认情况下,R假定CSV文件是包含一个头行的,这行主要包含数据集的所有特性名称。如果CSV文件不包含的头行的话,需要在读取函数中指明如图4)
|
|