0823
逻辑和控流
-- print("XXX" ) 得加括号
Loops循环
- While Loop
执行一个函数直至符合括号里的条件。注意进入无限死循环
> # Initialize the speed variable
> speed <- 64
>
> # Code the while loop
> while (speed>30) {
print("Slow down!")
speed <- speed-7
}
[1] "Slow down!"
[1] "Slow down!"
[1] "Slow down!"
[1] "Slow down!"
[1] "Slow down!"
>
> # Print out the speed variable
> speed
[1] 29
> # Initialize the speed variable
> speed <- 64
>
> # Extend/adapt the while loop
> while (speed > 30) {
print(paste("Your speed is",speed))
if (speed > 48 ) {
print("Slow down big time")
speed<- speed -11
} else {
print("Slow down!")
speed<- speed-6
}
}
[1] "Your speed is 64"
[1] "Slow down big time"
[1] "Your speed is 53"
[1] "Slow down big time"
[1] "Your speed is 42"
[1] "Slow down!"
[1] "Your speed is 36"
[1] "Slow down!"
新函数,paste( ), while下的if else
- break
> # Initialize the speed variable
> speed <- 88
>
> while (speed > 30) {
print(paste("Your speed is", speed))
# Break the while loop when speed exceeds 80
if (speed >80) {
break
}
if (speed > 48) {
print("Slow down big time!")
speed <- speed - 11
} else {
print("Slow down!")
speed <- speed - 6
}
}
[1] "Your speed is 88"
# Initialize i as 1
i <- 1
# Code the while loop
while (i <= 10) {
print(3*i)
if (3*i%%8==0 ) {
break
}
i <- i + 1
}
- for loop
> # The linkedin vector has already been defined for you
> linkedin <- c(16, 9, 13, 5, 2, 17, 14)
> # Loop version 1
> for(p in linkedin) {
print(p)
}
[1] 16
[1] 9
[1] 13
[1] 5
[1] 2
[1] 17
[1] 14
>
> # Loop version 2
> for(i in 1:length(linkedin)){
print(linkedin[i])
}
[1] 16
[1] 9
[1] 13
[1] 5
[1] 2
[1] 17
[1] 14
list也一样,但是要用到"[[ ]]"
- 双重for loop
> # define the double for loop
> for (i in 1:nrow(ttt)) {
for (j in 1:ncol(ttt)) {
print(paste( "On row", i, "and column", j ,"the board contains",ttt[i,j]))
}
}
[1] "On row 1 and column 1 the board contains O"
[1] "On row 1 and column 2 the board contains NA"
[1] "On row 1 and column 3 the board contains X"
[1] "On row 2 and column 1 the board contains NA"
[1] "On row 2 and column 2 the board contains O"
[1] "On row 2 and column 3 the board contains O"
[1] "On row 3 and column 1 the board contains X"
[1] "On row 3 and column 2 the board contains NA"
[1] "On row 3 and column 3 the board contains X"
for loop 与 if break next的连用
> # The linkedin vector has already been defined for you
> linkedin <- c(16, 9, 13, 5, 2, 17, 14)
>
> # Adapt/extend the for loop
> for (li in linkedin) {
if (li > 10) {
print("You're popular!")
} else {
print("Be more visible!")
}
# Add if statement with break
if(li > 16){
print("This is ridiculous, I'm outta here!" )
break
}
# Add if statement with next
if(li <5) {
print( "This is too embarrassing!")
next
}
print(li)
}
[1] "You're popular!"
[1] 16
[1] "Be more visible!"
[1] 9
[1] "You're popular!"
[1] 13
[1] "Be more visible!"
[1] 5
[1] "Be more visible!"
[1] "This is too embarrassing!"
[1] "You're popular!"
[1] "This is ridiculous, I'm outta here!"
> # Pre-defined variables
> rquote <- "r's internals are irrefutably intriguing"
> chars <- strsplit(rquote, split = "")[[1]]
> # Initialize rcount
> rcount <- 0
> # Finish the for loop
> for (char in chars) {
if(char=="r"){
rcount<- rcount+1
}
if(char=="u"){
break
}
}
> rcount
[1] 5
So, if 函数可以单独分开用?
Functions
- 自编function
-- 结果是print还是return有点搞不清
> # Create a function pow_two()
> pow_two <- function(x){
x<-x^2
print(x)
}
>
>
> # Use the function
> pow_two(12)
[1] 144
>
> # Create a function sum_abs()
> sum_abs<- function(x,y){
a<-sum(abs(x),abs(y))
return(a)
}
>
> sum_abs(-2,3)
[1] 5
有时候需要return,有时候又不需要。或者有时候加不加return都不会影响结果,为什么
计算结束,返回结果?!!
Apply家族
0824
- lapply
可以根据function指定计算参数?
multiply <- function(x, factor) {
x * factor
}
lapply(list(1,2,3), multiply, factor = 3)
pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
split <- strsplit(pioneers, split = ":")
新函数,strsplit ( )
- sapply
sapply 返回的是一个数组,
lapply返回的是一个list
新函数 cat( ),
cat("The average temperature is", mean(x), "\n")
- apply
working on the temp list again, that contains 7 numerical vectors of length 5. We also coded a function basics() that takes a vector, and returns a named vector of length 3, containing the minimum, mean and maximum value of the vector respectively.
> # temp is already available in the workspace
>
> # Definition of basics()
> basics <- function(x) {
c(min = min(x), mean = mean(x), max = max(x))
}
>
> # Apply basics() over temp using vapply()
> vapply(temp,basics,numeric(3))
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
min -1.0 5 -3.0 -2.0 2.0 -3.0 1.0
mean 4.8 9 2.2 2.4 5.4 4.6 4.6
max 9.0 13 8.0 7.0 9.0 9.0 9.0
numeric(3) 返回长度为3的数字数据???
logical( 1 )返回长度为1的逻辑数据???
vapply(temp, function(x, y) { mean(x) > y }, y = 5,logical(1))
常用功能
rev()
> vec1 <- c(1.5, 2.5, 8.4, 3.7, 6.3)
> vec2 <- rev(vec1)
> vec2
[1] 6.3 3.7 8.4 2.5 1.5
append()
> linkedin <- list(16, 9, 13, 5, 2, 17, 14)
> facebook <- list(17, 7, 5, 16, 8, 13, 14)
> li_vec<-as.vector(linkedin)
> fb_vec<-as.vector(facebook)
> social_vec<- append(fb_vec,li_vec)
# The linkedin and facebook lists have already been created for you
linkedin <- list(16, 9, 13, 5, 2, 17, 14)
facebook <- list(17, 7, 5, 16, 8, 13, 14)
# Convert linkedin and facebook to a vector: li_vec and fb_vec
li_vec<-as.vector(linkedin)
fb_vec<-as.vector(facebook)
# Append fb_vec to li_vec: social_vec
social_vec<- as.vector(append(li_vec,fb_vec))
# Sort social_vec
sort(unlist(social_vec),decreasing=T)
rep()
seq()
# Create first sequence: seq1
seq1<-seq(1,500,by=3)
# Create second sequence: seq2
seq2<-seq(1200,900,by=-7)
常用regex
grepl()
pattern="^a" 首字母是"a"
pattern="a$有复数个"a"

grep()

# Use grepl() to match for .edu addresses more robustly
grepl("@.*\\.edu",emails)
# Use grep() to match for .edu addresses more robustly, save result to hits
hits<- grep("@.*\\.edu",emails)
sub()
sub 换一个
gsub()
全换


> emails <- c("john.doe@ivyleague.edu", "education@world.gov", "global@peace.org","invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
>
> # Use sub() to convert the email domains to datacamp.edu
> sub("@.*\\.edu$","@datacamp.edu",emails)
[1] "john.doe@datacamp.edu" "education@world.gov"
[3] "global@peace.org" "invalid.edu"
[5] "quant@datacamp.edu" "cookie.monster@sesame.tv"


A vector of character strings containing
"Won 1 Oscar.", "24", "2", "3", "2", "1".
The ([0-9]+) selects the entire number that comes before the word “nomination” in the string, and the entire match gets replaced by this number because of the \1 that reference to the content inside the parentheses.
0828
Time and date
- 指定格式
as.Date("1971-14-05",format="%Y-%d-%m")
[1] "1971-05-14"

> # Definition of character strings representing dates
> str1 <- "May 23, '96"
> str2 <- "2012-03-15"
> str3 <- "30/January/2006"
>
> # Convert the strings to dates: date1, date2, date3
> date1 <- as.Date(str1, format = "%b %d, '%y")
> date2 <- as.Date(str2,format="%Y-%m-%d")
> date3 <- as.Date(str3,format="%d/%B/%Y")
>
> # Convert dates to formatted strings
> format(date1, "%A")
[1] "Thursday"
> format(date2, "%d")
[1] "15"
> format(date3, "%b %Y")
[1] "Jan 2006"

> # Convert the strings to POSIXct objects: time1, time2
> time1 <- as.POSIXct(str1, format = "%B %d, '%y hours:%H minutes:%M seconds:%S")
> time2 <- as.POSIXct(str2, format = "%Y-%m-%d %H:%M:%S")
>
> # Convert times to formatted strings
> format(time1,"%M")
[1] "01"
> format(time2,"%I:%M %p")
[1] "02:23 PM"
以上
网友评论