ImageVerifierCode 换一换
格式:PPT , 页数:48 ,大小:304KB ,
资源ID:376710      下载积分:2000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
注意:如需开发票,请勿充值!
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-376710.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Introduction to R- Lesson 2 - Manipulating Data.ppt)为本站会员(吴艺期)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

Introduction to R- Lesson 2 - Manipulating Data.ppt

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?,

13、Examining Data,What class of data is id? dog_type?, class(dat$id) 1 “integer“ class(dat$dog_type) 1 “character“,Examining Data,What class of data is id? dog_type?, str(dat) data.frame: 1000 obs. of 7 variables:$ id : int 1 2 3 4 5 6 7 8 9 10 .$ age : int 40 36 69 56 66 84 40 73 76 38 .$ sex : chr “F

14、“ “M“ “M“ “F“ .$ height : num 63.5 65.6 68.2 62.9 63.7 70.8 67 67 62.6 62.2 .$ weight : num 134 192 170 134 133 .$ dog : chr “no“ “no“ “no“ “no“ .$ dog_type: chr NA NA NA NA .,Examining Data,How many total participants are there? How many men and how many women?,Examining Data,How many total partici

15、pants are there? How many men and how many women?, length(unique(dat$id) 1 1000 unique(c(1,1,2,2,3) 1 1 2 3 length(unique(c(1,1,2,2,3) 1 3 length(c(1,1,2,2,3) 1 5,Examining Data,How many total participants are there? How many men and how many women?, table(dat$sex)F M 493 507,Examining Data,How many

16、 people have dogs?,Examining Data,How many people have dogs?, table(dat$dog)no yes 518 482,Examining Data,How many different types of dogs are there? How many of each?,Examining Data,How many different types of dogs are there? How many of each?, table(dat$dog_type)husky lab poodle retriever 113 125

17、111 133,Overview,Importing Data Examining Data Recoding Variables Exporting Data,Recoding Data,Missingness: represented by NA default read.table(,na.strings = “NA“,) you can change based on your data NA is NOT a character string:, x = rep(NA,3) x 1 NA NA NA class(x) 1 “logical“,Recoding Data,NA valu

18、es are essentially ignored, except when you use certain functions, x = c(NA, 1, NA, 3, 4) x*2 1 NA 2 NA 6 8 mean(x) 1 NA mean(x, na.rm = TRUE) 1 2.666667,Recoding Data,is.na() tests for missing entries Returns TRUE or FALSE at each entry, x = c(NA, 1, NA, 3, 4) x 1 NA 1 NA 3 4 class(x) 1 “numeric“ i

19、s.na(x) 1 TRUE FALSE TRUE FALSE FALSE,Recoding Data,which() returns the indices for entries that are TRUE, which(is.na(x) 1 1 3,Recoding Data,! means not:, which(!is.na(x) 1 2 4 5 x 1 NA 1 NA 3 4 Index = which(!is.na(x) xIndex 1 1 3 4,Recoding Data,which is implicit when you subset using is.na (or !

20、is.na),# in one step x!is.na(x) 1 1 3 4,Recoding Data,Renaming binary variables ex: change sex from M/F to 0/1, head(dat$sex) 1 “F“ “M“ “M“ “F“ “F“ “M“ bin.sex = ifelse(dat$sex=“F“,1,0) head(bin.sex) 1 1 0 0 1 1 0,Recoding Data,?ifelse: ifelse(test, yes, no) test - an object which can be coerced to

21、logical mode (ie TRUE or FALSE) yes - return values for true elements of test no - return values for false elements of test,Recoding Data,Logical characters: =, !=, , = Also: is.type ie: is.na, is.character, is.data.frame, is.numeric, etc, x = c(1,3,7,9) x 3 1 FALSE FALSE TRUE TRUE x = 3 1 FALSE TRU

22、E FALSE FALSE,Recoding Data, bin.sex = ifelse(dat$sex=“F“,1,0) head(dat$sex = “F“) 1 TRUE FALSE FALSE TRUE TRUE FALSE head(bin.sex) 1 1 0 0 1 1 0,Recoding Data,Analogously, creating a cut-point in continuous data:, head(dat$age) 1 40 36 69 56 66 84 bin.age = ifelse(dat$age head(bin.age) 1 0 0 1 1 1

23、1,Overview,Importing Data Examining Data Recoding Variables Exporting Data,Exporting Data,write.table(data, filename, quote = T, row.names = T, col.names = T, sep = “ “) data is an R object ie dat in our case filename is similar to read.table you should include a .txt in the filename quote puts char

24、acter strings in quotes (I like setting that to be FALSE or F),Exporting Data,row.names: includes the row.names in the output, which is usually just a sequence from 1 to nrow(dat) I prefer FALSE, as excel automatically has row indices col.names: include the header names in the output file? Depending on the data, I usually use TRUE,Practice,Make a 2 x 2 table of sex and dog Create a BMI variable using height and weight Hint: BMI = weightlbs*703/(heightin)2 Create an overweight variable, which gives the value 1 for people with BMI 30 and 0 otherwise,

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1