欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    Introduction to R Lecture 3- Data Manipulation.ppt

    • 资源ID:376709       资源大小:163.50KB        全文页数:44页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Introduction to R Lecture 3- Data Manipulation.ppt

    1、Introduction to R Lecture 3: Data Manipulation,Andrew Jaffe 9/27/10,Overview,Practice Solutions Indexing Data Management Data Summaries,Practice,Make a 2 x 2 table of sex and dog, table(dat$sex, dat$dog)no yesF 264 229M 254 253,Practice,Create a BMI variable using height and weight, dat$bmi = dat$we

    2、ight*703/dat$height2 head(dat$bmi) 1 23.44931 31.29991 25.69422 23.89881 23.11172 28.13324,Practice,Create an overweight variable, which gives the value 1 for people with BMI 30 and 0 otherwise, dat$overweight = ifelse(dat$bmi 30, 1, 0) head(dat$overweight) 1 0 1 0 0 0 0,Practice,Add those two varia

    3、bles to the datasets and save it as a text file somewhere,write.table(dat, “lec2_practice.txt“, quote = F, row.names = F, sep=“t“),Overview,Practice Solutions Indexing Data Management Data Summaries,Indexing,Vectors: vectorindex takes index elements from vector and returns them, x = c(1,3,7,34,435)

    4、x1 1 1 xc(1,4) 1 1 34 x2:4 1 3 7 34, 2:4 1 2 3 4,Indexing,Replace elements in a vector combining indexing, is.na(), and rep(), x = c(1,3,NA,6,NA,8) which(is.na(x) 1 3 5 xis.na(x) = 0 # or rep(0) x 1 1 3 0 6 0 8,Indexing,Data.frames/matrices: datrow,col Can subset/extract a row: datrow, Can subset/ex

    5、tract a column: dat,col, x = matrix(c(1,2,3,4,5,6), ncol = 3) x,1 ,2 ,3 1, 1 3 5 2, 2 4 6,Indexing, x1, 1 1 3 5 x,1 1 1 2 x1,1 1 1 x1:2,1:2,1 ,2 1, 1 3 2, 2 4, x,1 ,2 ,3 1, 1 3 5 2, 2 4 6,Indexing, x1, = rep(1) x,1 ,2 ,3 1, 1 1 1 2, 2 4 6 x,1 = rep(2) x,1 ,2 ,3 1, 2 1 1 2, 2 4 6, x,1 ,2 ,3 1, 1 3 5

    6、2, 2 4 6,Overview,Practice Solutions Indexing Data Management Data Summaries,Data Management,An aside: save() and load() save(obj_1,obj_n, file = “filename.rda”) Saves R objects (vectors, matrices, or data.frames) as an .rda file (similar to .dta) load(“filename.rda”) Loads whatever files were saved

    7、 in the .rda Easier than reading/writing tables,Data Management,Your workspace can be saved an .rda file You get asked this every time you close R save.image(“filename.Rdata”) saves all objects in your workspace (what ls() returns) Each folder might have its own .Rdata file Doing this is personal pr

    8、eference - if you have a script and its a quick analysis, probably dont need a saved image,Data Management,“lec3_data.rda” can be downloaded from the website Similar method to read in the data: load(“lec3_data.rda”) Put in the same directory as your script Set your working directory Use the full fil

    9、ename,Data Management,What are the dimensions of the dataset?,Data Management,What are the dimensions of the dataset?, dim(dog_dat) 1 482 6,Data Management,How many dogs are in this dataset? Is this dataset unique?,Data Management,How many dogs are in this dataset? Is this dataset unique?, length(un

    10、ique(dog_dat$dog_id) 1 482 length(dog_dat$dog_id) 1 482,Data Management,What are the column/variable names?,Data Management,What are the column/variable names?, head(dog_dat)dog_id owner_id dog_type dog_wt_mo1 dog_len_mo1 dog_food_mo1 1 1 394 lab 51.5 13.8 25.8 2 2 571 lab 48.3 24.6 33.1 3 3 986 poo

    11、dle 59.3 22.7 29.2 4 4 750 lab 46.4 22.3 27.6 5 5 882 husky 48.0 20.9 28.0 6 6 762 poodle 47.0 19.1 31.0, names(dog_dat) 1 “dog_id“ “owner_id“ “dog_type” “dog_wt_mo1“ 5 “dog_len_mo1“ “dog_food_mo1“,Data Management,Some explanation of the variables dog_id: id of dog owner_id: id of owner dog_type: ty

    12、pe of dog dog_wt_mo1: dog weight at month 1 (baseline) dog_len_mo1: dog length at month 1 dog_food_mo1: baseline dog food consumption,Data Management,Subsetting data: separate data into two data.frames based on a variable:, lab = dog_datdog_dat$dog_type = “lab“, head(lab)dog_id owner_id dog_type dog

    13、_wt_mo1 dog_len_mo1 dog_food_mo1 1 1 394 lab 51.5 13.8 25.8 2 2 571 lab 48.3 24.6 33.1 4 4 750 lab 46.4 22.3 27.6 7 7 664 lab 53.0 18.2 25.7 13 13 713 lab 48.3 23.4 31.8 15 15 480 lab 46.6 20.8 31.3,Data Management, lab = dog_datdog_dat$dog_type = “lab“, head(which(dog_dat$dog_type = “lab“) 1 1 2 4

    14、7 13 15,Taking those specific rows, and all of the columns of the original data,Data Management, lab2 = dog_datdog_dat$dog_type = ”lab“,1:3 head(lab2,3)dog_id owner_id dog_type 1 1 394 lab 2 2 571 lab 4 4 750 lab,Taking those specific rows, and the first 3 columns of the original data,Data Managemen

    15、t,Note (stata users) that we have two data.frames in our workspace! ls(),Data Management,Remember we used ifelse() for binary conversions?, heavy = ifelse(dog_dat,4 mean(dog_dat,4), 1, 0) head(heavy) 1 1 0 1 0 0 0,Note that you can use column indexing instead of $name for data.frames,This is just th

    16、e mean of that column: mean(dog_dat,4) 1 49.69606,Data Management,The cut() function can split data into more groups quintiles, tertiles, etc cut(dat, breaks) dat is a vector of numerical or integer values breaks is where to make the cuts,Data Management,If breaks is one number (n), it splits the da

    17、ta into n equal sized groups, x = 1:5 # 1 2 3 4 5 or seq(1,5) cut(x, 2) 1 (0.996,3 (0.996,3 (0.996,3 (3,5 (3,5 Levels: (0.996,3 (3,5 cut(x, 3) 1 (0.996,2.33 (0.996,2.33 (2.33,3.67 (3.67,5 (3.67,5 Levels: (0.996,2.33 (2.33,3.67 (3.67,5 cut(x,3, labels=F) # returns integers of groups, not factors 1 1

    18、1 2 3 3,FACTORS!,Data Management,What is a factor? Similar to terms like category and enumerated type Has levels associated with it could be ordinal if factor(,ordered = T) Must only have an as.character() method and be sortable to be converted to a factor using factor(),Data Management,If breaks ar

    19、e more than one number, splits the vector by those numbers, x = 1:10 cut(x, c(0,3,6,10)1 (0,3 (0,3 (0,3 (3,6 (3,6 (3,6 (6,10 (6,10 (6,10 (6,10 Levels: (0,3 (3,6 (6,10 cut(x, c(0,3,6,10), FALSE)1 1 1 1 2 2 2 3 3 3 3,Data Management,Something more applicable for cut: the quantile(x,probs) function - d

    20、efault probs is seq(0,1,0.25), ie quintiles seq(start, end, by) creates a sequence from the starting value, to the ending value by the specified amount seq(0,10) 0:10 # 0, 1, 2, , 9, 10 seq(0,10,0.5) # 0, 0.5, 1.0, , 9.5, 10.0,Data Management,Now for stuff with our data:, quantile(dog_dat$dog_wt_mo1

    21、)0% 25% 50% 75% 100% 10.600 44.600 49.200 55.275 72.500 quantile(dog_dat$dog_wt_mo1, seq(0,1,0.5)0% 50% 100% 10.6 49.2 72.5 quantile(dog_dat$dog_wt_mo1, 0.6)60% 51.5 quantile(dog_dat$dog_wt_mo1, c(0.4,0.6)40% 60% 47.24 51.50,Data Management, sp = quantile(dog_dat$dog_wt_mo1, 0.75) big = ifelse(dog_d

    22、at$dog_wt_mo1 sp, 1, 0) head(big) 1 0 0 1 0 0 0, quant = cut(dog_dat$dog_wt_mo1, quantile(dog_dat$dog_wt_mo1) head(quant) 1 (49.2,55.3 (44.6,49.2 (55.3,72.5 (44.6,49.2 (44.6,49.2 (44.6,49.2 Levels: (10.6,44.6 (44.6,49.2 (49.2,55.3 (55.3,72.5,Overview,Practice Solutions Indexing Data Management Data

    23、Summaries,Data Summaries,This is some of the only “statistics” in the course R functions can perform statistics well, here are some basics for summaries,Data Summaries,mean(dat, na.rm = F) median(dat, na.rm=F), x = c(1,2,4,6,NA) mean(x) 1 NA mean(x, na.rm=T) 1 3.25 median(x,na.rm=T) 1 3,Data Summari

    24、es, x = c(1,2,4,7,9,11) mean(x) 1 5.666667 median(x) 1 5.5 var(x) 1 15.86667 sd(x) 1 3.983298,Data Summaries,Lets combine some concepts! Take the mean food consumption of all of the labs,Data Summaries,First, figure out which entries correspond to dogs that are labs, Index = which(dog_dat$dog_type =

    25、 “lab“) head(Index) 1 1 2 4 7 13 15,Data Summaries,Then, take the mean of the data you want, mean(dog_dat$dog_food_mo1Index) 1 30.04,Note that we first created a vector of dog food, then indexed it - there are no commas needed for the indexing (because its a vector),Data Summaries,Combined into 1 li

    26、ne/command:, mean(dog_dat$dog_food_mo1dog_dat$dog_type = “lab“) 1 30.04, mean(dog_datdog_dat$dog_type = “lab“,6) 1 30.04 mean(dog_datdog_dat$dog_type = “lab“,“dog_food_mo1“) 1 30.04,Pick your favorite theyre all the same! Note that the first option might make the most sense,Practice,Compute the average dog weight, dog length, and dog food consumption for each dog type at baseline Reminder: the dog types are lab, poodle, husky, and retriever,


    注意事项

    本文(Introduction to R Lecture 3- Data Manipulation.ppt)为本站会员(吴艺期)主动上传,麦多课文档分享仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文档分享(点击联系客服),我们立即给予删除!




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

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

    收起
    展开