welcome: please sign in
location: attachment:rstuff.r von RstatisTik/RstatisTikPortal/RcourSeSep

Dateianhang 'rstuff.r'

Herunterladen

   1 setwd("/media/mandy/Samsung_T1/mpicbs/2015kurs2")
   2 
   3 
   4 ##################################################
   5 ################ First Session ###################
   6 ##################################################
   7 x <- rnorm(100, mean = 2, sd = 4)
   8 
   9 xx <- density(x)
  10 
  11 plot(x)
  12 
  13 ls()
  14 objects()
  15 
  16 library()
  17 head(installed.packages)
  18 
  19 
  20 ##################################################
  21 ############## Objects and Functions #############
  22 ##################################################
  23 
  24 
  25 ### str()
  26 require(ggplot2)
  27 example(scale_colour_brewer)
  28 
  29 ls()
  30 str(dsamp)
  31 str(d)
  32 
  33 ##################################################
  34 ################### Vectors  #####################
  35 ##################################################
  36 
  37 ## Coercion - Exercises
  38 c(1, FALSE)
  39 c("a", 1)
  40 c(list(1), "a")
  41 c(TRUE, 1L)
  42 
  43 1 == "1"
  44 -1 < FALSE
  45 "one" < 2
  46 
  47 
  48 v1 <- c(1L,1L,2L)
  49 v2 <- seq(0,5,by = 0.5)
  50 v3 <- rep(c(F,T),times = c(2,5))
  51 v4 <- letters[sample(1:5)]
  52 
  53 length(v1)
  54 length(v2)
  55 
  56 lapply(list(v1,v2,v3,v4), typeof)
  57 
  58 
  59 
  60 
  61 ## What happens to a factor when you modify its levels?
  62 f1 <- factor(letters)
  63 summary(f1)
  64 levels(f1) <- rev(levels(f1))
  65 summary(f1)
  66 
  67 f2 <- rev(factor(letters))
  68 summary(f2)
  69 
  70 f3 <- factor(letters, levels = rev(letters))
  71 summary(f3)
  72 
  73 f4 <- c(f2,f3)
  74 summary(f4)
  75 
  76 
  77 ##################################################
  78 ################ Data Frames  ####################
  79 ##################################################
  80 
  81 ## combining data frames
  82 ### rbind
  83 
  84 x <- data.frame(id=1:3,score=rnorm(3))
  85 y <- data.frame(id=13:15,score=rnorm(3))
  86 rbind(x,y)
  87 
  88 
  89 ## cbind
  90 x <- data.frame(id=1:5,score1=rnorm(5))
  91 y <- data.frame(score2=rnorm(5),score3=rnorm(5))
  92 cbind(x,y)
  93 
  94 
  95 ## merge
  96 (d1 <- data.frame(id=LETTERS[c(1,2,3)],day1=sample(10,3)))
  97 (d2 <- data.frame(id=LETTERS[c(1,3,5,6)],day2=sample(10,4)))
  98 
  99 merge(d1,d2)
 100 
 101 merge(d1,d2,all.x = T)
 102 
 103 merge(d1,d2,all.y = T)
 104 
 105 zz <- merge(d1,d2,all = T)
 106 
 107 
 108 ## exercises merging
 109 
 110 m1 <- data.frame(id = 1:10,
 111                  m1 = rnorm(10),
 112                  m2 = runif(10))
 113 
 114 m2 <- data.frame(id2 = sample(1:20,size = 10),
 115                  m2 = rnorm(10, mean = 5),
 116                  m3 = rt(10,df = 4))
 117 
 118 require(lubridate)
 119 subjdata <- data.frame(id = 1:20,
 120                        bd = ymd("19900101") + ddays(sample.int(3500)),
 121                        sex = sample(c("m","f"),20,replace = T))
 122 
 123 
 124 ## indexing 
 125 
 126 ## indexing with positive integers
 127 letters[1:3]
 128 letters[c(1:3,1:3)]
 129 
 130 
 131 ## logical indexing
 132 x <- c(1,2,3,NA)
 133 x[!is.na(x)]
 134 
 135 ## indexing in combination with assignment
 136 x[is.na(x)] <- 0
 137 x
 138 
 139 ## logical indexing on data frames
 140 library(MASS)
 141 painters[painters$Colour >= 17,]
 142 
 143 ## logical indexing on data frames (rows) combined with indexing with integers (columns)
 144 painters[painters$Colour >= 17 & painters$Composition > 10, c(1,2,3)]
 145 
 146 
 147 ## use of %in%
 148 painters[painters$School %in% c("A","C","D"),]
 149 
 150 ## use of which
 151 which(painters$School %in% c("A","C","D"))
 152 
 153 ## indexing with character vector
 154 painters[1:3,c("Drawing","Expression")]
 155 
 156 ## negative indeces
 157 x <- c(5,8,6,7,1,5,3)
 158 (z <- x[-1])
 159 
 160 
 161 ##########################################################
 162 ###################### Exercises Indexing ################
 163 ##########################################################
 164 
 165 ########## part A
 166 
 167 ## run the following commands!
 168 ## try to understand whats going on in each case!
 169 x <- c(2, 7, 0, 9, 10, 23, 11, 4, 7, 8, 6, 0)
 170 x[4]
 171 x[3:5]
 172 x[c(1, 5, 8)]
 173 x[x > 10]
 174 x[(1:6) * 2]
 175 all(x > 0)
 176 x[x == 0] <- 1
 177 x
 178 
 179 all(x > 0)
 180 ifelse(round(x/2) == x/2, "even", "odd")
 181 
 182 sum(x > 4)
 183 
 184 ## Display every third element in x
 185 
 186 
 187 
 188 ## Display elements that are less than 10,
 189 ## but greater than 4
 190 
 191 
 192 
 193 ## Modify the vector x, replacing by 10 all values
 194 ## that are greater than 10
 195 
 196 
 197 
 198 ## Modify the vector x, multiplying by 2 all
 199 ## elements that are smaller than 5
 200 
 201 
 202 
 203 ## Create a new vector y with elements 0,1,0,1, . . .
 204 ## (12 elements) and a vector z that equals x when y=0
 205 ## and 3x when y=1.
 206 ## (You can do it using ifelse, but there are other possibilities)
 207 
 208 
 209 
 210 ##################################################
 211 ################ Reading Data ####################
 212 ##################################################
 213 
 214 read.table("week1/data/fishercats.txt",  
 215            sep=" ",header=T)
 216 
 217 
 218 winer <- read.table( 
 219     "http://socserv.socsci.mcmaster.ca/jfox/Courses/R/ICPSR/Winer.txt",
 220     header=T)
 221 
 222 
 223 mydata <- read.delim("clipboard",na.strings=".")
 224 
 225 
 226 data(airquality)
 227 aq <-edit(airquality)
 228 
 229 fix(airquality)
 230 
 231 ## read excel
 232 library(XLConnect)
 233 my.wb <- loadWorkbook("week1/data/Duncan.xls")
 234 sheets <- getSheets(my.wb)
 235 content <- readWorksheet(my.wb, sheet=1)
 236 head(content)
 237 
 238 require(readxl)
 239 x <- read_excel("week1/data/Duncan.xls")
 240 
 241 
 242 ##################################################
 243 ################ Apply family ####################
 244 ##################################################
 245 
 246 lapply(mtcars,mean)
 247 sapply(mtcars,mean)
 248 
 249 
 250 x <- 1:12
 251 dim(x) <- c(2,2,3)
 252 apply(x,3,quantile) #calculate the quantiles for each 2x2 matrix
 253 
 254 
 255 tapply(mtcars$mpg,mtcars$cyl,mean)
 256 
 257 tapply(mtcars$mpg,list(mtcars$cyl,mtcars$vs),mean)
 258 

Gespeicherte Dateianhänge

Um Dateianhänge in eine Seite einzufügen sollte unbedingt eine Angabe wie attachment:dateiname benutzt werden, wie sie auch in der folgenden Liste der Dateien erscheint. Es sollte niemals die URL des Verweises ("laden") kopiert werden, da sich diese jederzeit ändern kann und damit der Verweis auf die Datei brechen würde.
  • [laden | anzeigen] (2015-10-19 19:25:19, 6.2 KB) [[attachment:dataweek4.zip]]
  • [laden | anzeigen] (2015-10-26 10:49:21, 8.2 KB) [[attachment:dataweek5.zip]]
  • [laden | anzeigen] (2015-10-13 05:01:59, 2.9 KB) [[attachment:fakepersdat.rdata]]
  • [laden | anzeigen] (2015-11-10 07:40:26, 1.3 KB) [[attachment:plar.r]]
  • [laden | anzeigen] (2015-10-05 17:35:00, 1.1 KB) [[attachment:readdata.r]]
  • [laden | anzeigen] (2015-09-28 19:42:02, 4.9 KB) [[attachment:rstuff.r]]
  • [laden | anzeigen] (2015-10-13 10:11:57, 10.0 KB) [[attachment:rstuff2.r]]
  • [laden | anzeigen] (2015-10-05 17:37:33, 9.7 KB) [[attachment:rstuff2ex.r]]
  • [laden | anzeigen] (2015-10-13 05:37:06, 9.6 KB) [[attachment:rstuff3ex.r]]
  • [laden | anzeigen] (2015-10-13 10:11:42, 12.9 KB) [[attachment:rstuff3sol.r]]
  • [laden | anzeigen] (2015-10-19 19:25:32, 9.6 KB) [[attachment:rstuff4ex.r]]
  • [laden | anzeigen] (2015-11-03 06:37:52, 11.8 KB) [[attachment:rstuff4sol.r]]
  • [laden | anzeigen] (2015-10-27 08:07:10, 11.1 KB) [[attachment:rstuff5ex.r]]
  • [laden | anzeigen] (2015-11-03 06:34:59, 9.6 KB) [[attachment:rstuff6ex.r]]
  • [laden | anzeigen] (2015-11-03 10:59:26, 12.3 KB) [[attachment:rstuff6sol.r]]
  • [laden | anzeigen] (2015-11-10 07:40:17, 5.6 KB) [[attachment:rstuff7ex.r]]
  • [laden | anzeigen] (2015-09-28 19:41:50, 1167.2 KB) [[attachment:session1.pdf]]
  • [laden | anzeigen] (2015-10-05 17:36:54, 303.0 KB) [[attachment:session2.pdf]]
  • [laden | anzeigen] (2015-10-13 05:37:00, 285.2 KB) [[attachment:session3.pdf]]
  • [laden | anzeigen] (2015-10-19 19:26:08, 1151.7 KB) [[attachment:session4.pdf]]
  • [laden | anzeigen] (2015-10-26 10:50:14, 1867.6 KB) [[attachment:session5.pdf]]
  • [laden | anzeigen] (2015-11-03 06:35:42, 2039.1 KB) [[attachment:session6.pdf]]
  • [laden | anzeigen] (2015-11-10 07:38:04, 929.2 KB) [[attachment:session7.pdf]]
  • [laden | anzeigen] (2015-09-28 19:43:16, 10.2 KB) [[attachment:week1data.zip]]
  • [laden | anzeigen] (2015-10-05 17:36:29, 10.2 KB) [[attachment:week2data.zip]]
 Alle Dateien | Ausgewählte Dateien: löschen verschieben auf Seite copy to page

Sie dürfen keine Anhänge an diese Seite anhängen!