in RStudio console, use ctrl+L to clear console;
in RStudio editor, use ctrl+shift+C to add multiline comments to selected lines.
?lm will show help of function lm()
help.search("tree") will display a list of the functions which help pages mention “tree”. Note that if some packages have been recently installed, it may be useful to refresh the database used by help.search using the option rebuild (e.g., help.search("tree", rebuild = TRUE)).
When R is running, variables, data, functions are stored in the active memory of the computer in the form of objects which have a name. The name of an object must start with a letter and CAN include dots(.)
#The functions available to the user are stored in a library localised on the disk in a directory called R_HOME/library
R.home() will show R_HOME, in Ubuntu 19.10 tested, it's "/usr/lib/R", this directory contains packages of functions.
The package named base is in a way the core of R, each package has a directory called R with a file named like the package , for instance, for the package base, this is the file R_HOME/library/base/R/base, This file contains functions of the package.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# following example create&shows details of a frame:
# myFrame=data.frame(
# emp_id = c (1:5),
# emp_name = c("Rick","Dan","Michelle","Ryan","Gary")
# )
# ls.str(pat="myFrame")
#
# myFrame : 'data.frame': 5 obs. of 2 variables:
# $ emp_id : int 1 2 3 4 5
# $ emp_name: Factor w/ 5 levels "Dan","Gary","Michelle",..: 4 1 3 5 2
# if there are too many lines, use ls.str(pat="myFrame", max.level = -1) to hide details.
#To delete objects in memory, we use the function rm: rm(x) deletes the
#object x, rm(x,y) deletes both the objects x and y, rm(list=ls()) deletes all
#the objects in memory
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# > A <- "Gomphotherium"; compar <- TRUE; z <- -Inf
# > mode(A); mode(compar); mode(z); length(A)
# [1] "character"
# [1] "logical"
# [1] "numeric"
# [1] 1
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
read table from https://s3.amazonaws.com/assets.datacamp.com/blog_assets/test.txt :
1 6 a
2 7 b
3 8 c
4 9 d
5 10 e
url <- "https://s3.amazonaws.com/assets.datacamp.com/blog_assets/test.txt"
read.table(
url,
header = FALSE,
quote = "\"’",
colClasses = c("numeric","numeric","character"),
nrows = 2, #only read some rows
skip = 0, #start from the first row
check.names = TRUE, #checks that the variable|column names are valid
blank.lines.skip = TRUE,
comment.char = "" # no comment in this file
)
V1 V2 V3
1 1 6 a
2 2 7 b
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
use scan to read table:
scan(url, n = 2, blank.lines.skip = TRUE, comment.char="#")
Read 2 items
[1] 1 6
#sep = "" , not " "
scan(url, sep = "", what = list(0,0,""), nmax=2)
Read 2 records
[[1]]
[1] 1 2
[[2]]
[1] 6 7
[[3]]
[1] "a" "b"
//into 3 vectors|variables
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sequence(c(3, 2, 4))
[1] 1 2 3 1 2 1 2 3 4
sequence(1:3)
[1] 1 1 2 1 2 3
seq(1, 5, 0.5)
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
The function gl (generate levels) is very useful because it generates regular
series of factors.
> gl(3, 5, length=30)
[1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
Levels: 1 2 3
> gl(2, 6, label=c("Male", "Female"))
[1] Male Male Male Male Male Male
[7] Female Female Female Female Female Female
Levels: Male Female
> expand.grid(h=c(60,80), w=c(100, 300), sex=c("Male", "Female"))
h w sex
1 60 100 Male
2 80 100 Male
3 60 300 Male
4 80 300 Male
5 60 100 Female
6 80 100 Female
7 60 300 Female
8 80 300 Female
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> matrix(1:6, 2, 3)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> matrix(1:6, 2, 3, byrow=TRUE)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> fac <- factor(c(1, 10))
> fac
[1] 1 10
Levels: 1 10
> as.numeric(fac)
[1] 1 2
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> x <- matrix(1:6, 2, 3)
> x
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> x[, 3]
[1] 5 6
> x[, 3, drop = FALSE]
[,1]
[1,] 5
[2,] 6
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> x <- 1:10
> x[x >= 5] <- 20
> x
[1] 1 2 3 4 20 20 20 20 20 20
> x[x == 1] <- 25
> x
[1] 25 2 3 4 20 20 20 20 20 20
No comments:
Post a Comment