Introduction to R- Lesson 2 - Manipulating Data.ppt
《Introduction to R- Lesson 2 - Manipulating Data.ppt》由会员分享,可在线阅读,更多相关《Introduction to R- Lesson 2 - Manipulating Data.ppt(48页珍藏版)》请在麦多课文档分享上搜索。
1、Introduction to R: Lesson 2 - Manipulating Data,Andrew Jaffe 9/13/10,Reminder,Here is the course website: http:/www.biostat.jhsph.edu/ajaffe/rseminar.html There is a running collection of functions that we have covered in class,Dataset,For the remaining sessions, were going to learn R by using data
2、from the Baltimore Dog Study Data collection is ongoing, and dataset will be updated weekly,http:/ Data Examining Data Recoding Variables Exporting Data,Importing Data,Here is a link to the data: http:/www.biostat.jhsph.edu/ajaffe/files/lecture_2_data.csv So how do we get it into R? Two options! Bot
3、h involve read.table(),Importing Data,read.table(filename, header = F, sep = “, as.is = !stringsAsFactors, ) In functions, “ means additional parameters can be passed/used These are some of the options associated with this functions all can be seen typing ?read.table in the console,Importing Data,fi
4、lename: the path to your file, in quotes If no path is specified (ie “C:Docsdata.txt“ or “UsersAndrewdata.txt“), then R will look in your working directory for the file (ie “data.txt“) For PCs, you need double backslashes to designate paths (ie “C:Docsdata.txt“) Basically, a single backslash is the
5、escape character,Importing Data,filename - you can: Write out the full file path using quotes and the correct syntax Manually set your working directory to where your script and files are located setwd() Or, if your script and files are in the same place, use Notepad+. It sets the scripts location t
6、o be the working directory,Importing Data,header default is false Does the first row of your file contain column names? If so, include header = T in your read.table() call,Importing Data,sep = “ what character separates columns? The escape character followed by the delimiter is used here: Tab: “t“ N
7、ewline/Enter/Return: “n“ Ampersand: “&“, etc,Importing Data,CSV is an exception A special case of read.table() exists: read.csv(), which takes all of the same parameters, except defaults sep = “,“ Analogously, read.delim() defaults sep = “t“,Importing data,as.is = F (as stringsAsFactors=T) : should
8、character strings be treated as factors? I prefer character strings as characters (ie as.is = T) and not factors Easier to manipulate, search, and match You can always change to factors later,Importing Data,Lets open up a new script: Notepad+ : File New Mac: File New Document Save it somewhere you c
9、an find later Write a header (using #) If Mac, use setwd() and include the folder you put the script,Importing Data,Lets get our data R Option 1: remember scan from last session? file = “http:/www.biostat.jhsph.edu/ajaffe/files/lecture_2_data.csv“,Importing Data,Option 2: Right click on the link to
10、the data on the webpage, and save it as a csv file in the same folder as your script file = “lecture_2_data.csv“,Importing Data,Either way: dat - read.csv(file, header = T, as.is=T),Overview,Importing Data Examining Data Recoding Variables Exporting Data,Examining Data,What are the dimensions of the
11、 dataset?,Examining Data,What are the dimensions of the dataset? dim(dat) 1 1000 7,Rows,Columns,Examining Data,What variables are included? What are their names?,Examining Data,What variables are included? What are their names?, head(dat)id age sex height weight dog dog_type 1 1 40 F 63.5 134.5 no 2
12、 2 36 M 65.6 191.6 no 3 3 69 M 68.2 170.0 no 4 4 56 F 62.9 134.5 no 5 5 66 F 63.7 133.4 no 6 6 84 M 70.8 200.6 no ,Examining Data,What variables are included? What are their names?, names(dat) 1 “id“ “age“ “sex“ “height“ 5 “weight“ “dog“ “dog_type“,Examining Data,What class of data is id? dog_type?,
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- INTRODUCTIONTORLESSON2MANIPULATINGDATAPPT
