gcoulibaly / WEEK2-_QUIZ_IS607

0 stars 0 forks source link

Quiz #3

Open gcoulibaly opened 10 years ago

gcoulibaly commented 10 years ago

R version 3.1.1 (2014-07-10) -- "Sock it to Me" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.

[Workspace loaded from ~/.RData]

1, creating numeric vector

v<-rep(1:5,4) v [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

2changing numeric vector into character

c<-as.character(v) c [1] "1" "2" "3" "4" "5" "1" "2" "3" "4" "5" "1" "2" [13] "3" "4" "5" "1" "2" "3" "4" "5"

3 changing numeric vector into factor vector

f<-factor(v) f [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 Levels: 1 2 3 4 5

5 number of levels that the vector has

l<nlevels(f) Error: object 'l' not found l<-nlevels(f) l [1] 5

5 Creating vector with a formula, sapply

r<-sapply(v, function(x) 3_x^2-4_x +1) r [1] 0 5 16 33 56 0 5 16 33 56 0 5 16 33 56 0 [17] 5 16 33 56

6 Creating a named list

7 Creating a named list

v=list(gender=c(1,2), status=c("single","married")) v $gender [1] 1 2

$status [1] "single" "married"

v[["gender"]] [1] 1 2 v$gender [1] 1 2

8 Creating a data frame

m<-data.frame(matrix(0, ncol=4, nrow =10)) m X1 X2 X3 X4 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 5 0 0 0 0 6 0 0 0 0 7 0 0 0 0 8 0 0 0 0 9 0 0 0 0 10 0 0 0 0

10, read a csv file

temperature= read.csv("temperature.csv") Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'temperature.csv': No such file or directory

11 read txt file from different directory

12setwd("") # set working directory

12 setwd("") # set working directory

setwd("C:/measurements.txt") Error in setwd("C:/measurements.txt") : cannot change working directory measurements = read.table("c":/measurments.txt") # read text file

gcoulibaly commented 10 years ago

question#13

factorial = function(n) {

  • f =1
  • for (i in 1:n)
  • f = f*i
  • f
  • } factoria;l(12)
gcoulibaly commented 10 years ago

question#13

factorial = function(n) {

  • f =1
  • for (i in 1:n)
  • f = f*i
  • f
  • } factoria;l(12)
gcoulibaly commented 10 years ago

question#13

factorial = function(n) {

  • f =1
  • for (i in 1:n)
  • f = f*i
  • f
  • } factoria;l(12)
gcoulibaly commented 10 years ago

question#13

factorial = function(n) {

  • f =1
  • for (i in 1:n)
  • f = f*i
  • f
  • } factoria;l(12) factorial(12) [1] 479001600
gcoulibaly commented 10 years ago

question#13

factorial = function(n) {

  • f =1
  • for (i in 1:n)
  • f = f*i
  • f
  • } factoria;l(12) factorial(12) [1] 479001600