Difference between revisions of "R"

From Crop Genomics Lab.
Jump to: navigation, search
Line 44: Line 44:
  
 
Lists
 
Lists
 +
# example of a list with 4 components -
 +
# a string, a numeric vector, a matrix, and a scaler
 +
w <- list(name="Fred", mynumbers=a, mymatrix=y, age=5.3)
 
  > w
 
  > w
 
  $name
 
  $name

Revision as of 08:46, 29 June 2014

data types Matrix

> y <- matrix(1:20,nrow=5,ncol=4)
> y
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20 

1에서 20까지의 숫자를 5개의 행, 4개의 열로 저장

Vector

> a <- c(1,2,5.3,6,-2,4) # numeric vector
> a
[1]  1.0  2.0  5.3  6.0 -2.0  4.0
> b <- c("one","two","three") # character vector
> b
[1] "one"   "two"   "three"
> c <- c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE) #logical vector
> c
[1]  TRUE  TRUE  TRUE FALSE  TRUE FALSE

Dataframe

> d <- c(1,2,3,4)
> e <- c("red", "white", "red", NA)
> f <- c(TRUE,TRUE,TRUE,FALSE)
> mydata <- data.frame(d,e,f)
> mydata
  d     e     f
1 1   red  TRUE
2 2 white  TRUE
3 3   red  TRUE
4 4  <NA> FALSE

Vector들을 옆으로 쭉 붙여놓는것,

> names(mydata) <- c("ID","Color","Passed")
> mydata
  ID Color Passed
1  1   red   TRUE
2  2 white   TRUE
3  3   red   TRUE
4  4  <NA>  FALSE

이름도 달 수 있다.

Lists

# example of a list with 4 components - 
# a string, a numeric vector, a matrix, and a scaler 
w <- list(name="Fred", mynumbers=a, mymatrix=y, age=5.3)
> w
$name
[1] "Fred"

$mynumbers
[1]  1.0  2.0  5.3  6.0 -2.0  4.0 

$mymatrix
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20

$age
[1] 5.3