This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
library(plyr)
Sys.setlocale("LC_ALL","Chinese")
[1] "LC_COLLATE=Chinese (Simplified)_China.936;LC_CTYPE=Chinese (Simplified)_China.936;LC_MONETARY=Chinese (Simplified)_China.936;LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_China.936"
movie =read.csv("movie_data.txt", header = TRUE, sep = "," , fileEncoding = "UTF-8")
attach(movie)
The following objects are masked from movie (pos = 3):
boxoffice, doubanscore, name, showtime, type
The following objects are masked from movie (pos = 4):
boxoffice, doubanscore, name, showtime, type
The following objects are masked from movie (pos = 7):
boxoffice, doubanscore, name, showtime, type
The following objects are masked from movie (pos = 8):
boxoffice, doubanscore, name, showtime, type
The following objects are masked from movie (pos = 9):
boxoffice, doubanscore, name, showtime, type
print(movie)
summary(movie)
name type showtime doubanscore boxoffice
Length:9 Length:9 Length:9 Min. :4.0 Min. : 6184
Class :character Class :character Class :character 1st Qu.:5.0 1st Qu.: 17799
Mode :character Mode :character Mode :character Median :6.4 Median : 78341
Mean :5.9 Mean : 95780
3rd Qu.:6.5 3rd Qu.:111694
Max. :7.7 Max. :338583
str(movie)
'data.frame': 9 obs. of 5 variables:
$ name : chr "叶问3" "美人鱼" "女汉子真爱公式" "西游记之孙悟空三打白骨精" ...
$ type : chr "动作" "喜剧" "喜剧" "喜剧" ...
$ showtime : chr "2016/3/4" "2016/2/8" "2016/3/18" "2016/2/8" ...
$ doubanscore: num 6.4 6.9 4.5 5.7 4 7.7 6.5 6.4 5
$ boxoffice : num 77064 338583 6184 119957 111694 ...
(movie = movie[order(movie$type,movie$doubanscore,decreasing = TRUE),])
(popular_type=ddply(movie, .(type), function(x){mean(x$boxoffice)}))
(popular_type=popular_type[order(popular_type$V1, decreasing = TRUE),])
popular_type
class(movie$boxoffice); class(movie$name)
[1] "numeric"
[1] "character"
movie$name[movie$type == "喜剧" & movie$doubanscore > 7]
[1] "功夫熊猫 3"
movie$showtime[movie$name =="美人鱼" | movie$name=="功夫熊猫 3"]
[1] "2016/1/29" "2016/2/8"
(m=as.Date(head(movie$showtime)))
[1] "2016-01-29" "2016-02-08" "2016-04-29" "2016-02-08" "2016-03-18" "2016-02-08"
format(m, format = "%B %d %Y")
[1] "一月 29 2016" "二月 08 2016" "四月 29 2016" "二月 08 2016" "三月 18 2016" "二月 08 2016"
format(m, format = "%B %d %Y %A")
[1] "一月 29 2016 星期五" "二月 08 2016 星期一" "四月 29 2016 星期五" "二月 08 2016 星期一" "三月 18 2016 星期五" "二月 08 2016 星期一"
format(m, format = "%B")
[1] "一月" "二月" "四月" "二月" "三月" "二月"
format(Sys.time(), format = "%Y/%B/%d %a %H:%M:%S")
[1] "2021/十月/04 周一 12:13:21"
library(lubridate)
x=c(20090101, "2009-01-02", "2009 01 03", "2009-1-4", "2009-1,5", "Created on 2009 1 6", "200901 !!! 07")
ymd(x)
[1] "2009-01-01" "2009-01-02" "2009-01-03" "2009-01-04" "2009-01-05" "2009-01-06" "2009-01-07"
mday(as.Date("2021-11-20"))
[1] 20
wday(as.Date("2021-11-20"))
[1] 7
begin = as.Date("2021-11-20")
end = as.Date("2021-12-20")
(during =end-begin)
Time difference of 30 days
difftime(end, begin, units = "weeks")
Time difference of 4.285714 weeks
difftime(end, begin, units = "hours")
Time difference of 720 hours
Sys.setlocale("LC_ALL","Chinese")
[1] "LC_COLLATE=Chinese (Simplified)_China.936;LC_CTYPE=Chinese (Simplified)_China.936;LC_MONETARY=Chinese (Simplified)_China.936;LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_China.936"
movie =read.csv("movie_data.txt", header = TRUE, sep = "," , fileEncoding = "UTF-8")
attach(movie)
The following objects are masked from movie (pos = 3):
boxoffice, doubanscore, name, showtime, type
The following objects are masked from movie (pos = 4):
boxoffice, doubanscore, name, showtime, type
The following objects are masked from movie (pos = 5):
boxoffice, doubanscore, name, showtime, type
The following objects are masked from movie (pos = 8):
boxoffice, doubanscore, name, showtime, type
The following objects are masked from movie (pos = 9):
boxoffice, doubanscore, name, showtime, type
The following objects are masked from movie (pos = 10):
boxoffice, doubanscore, name, showtime, type
as.Date(head(movie$showtime))
[1] "2016-03-04" "2016-02-08" "2016-03-18" "2016-02-08" "2016-02-08" "2016-01-29"
head(sort(as.Date(movie$showtime)))
[1] "2016-01-29" "2016-02-08" "2016-02-08" "2016-02-08" "2016-03-04" "2016-03-18"
head(movie[order(as.Date(movie$showtime)), c("name", "showtime")])
nchar(movie$name)
[1] 3 3 7 12 5 6 12 7 8
seq(1, 100, by=3)
[1] 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100
set.seed(100)
sample(1:100, 5)
[1] 74 89 78 23 86
paste0("number ", 1:10)
[1] "number 1" "number 2" "number 3" "number 4" "number 5" "number 6" "number 7" "number 8" "number 9" "number 10"
x=c(1,1,1,2,3,3)
x[5]
[1] 3
which(x==3)
[1] 5 6
which.max(x)
[1] 5
which.min(x)
[1] 1
intersect(c(1,2,3,4,5,6,7), c(2,4,1,5,6,9,5))
[1] 1 2 4 5 6
union(c("狗熊会","聚数据英才"), c("狗熊会","助产业振兴"))
[1] "狗熊会" "聚数据英才" "助产业振兴"
intersect(c("狗熊会","聚数据英才"), c("狗熊会","助产业振兴"))
[1] "狗熊会"
setdiff(10:2, c(11,8,9,100))
[1] 10 7 6 5 4 3 2
(Age=sample(20:100, 20,replace = T))
[1] 89 23 74 89 26 26 74 62 80 31 70 91 37 44 21 70 87 87 71 67
label=c("壮年","中年","长辈","老年")
(ages=cut(Age, breaks = c(20,30,50,70,100), labels = label))
[1] 老年 壮年 老年 老年 壮年 壮年 老年 长辈 老年 中年 长辈 老年 中年 中年 壮年 长辈 老年 老年 老年 长辈
Levels: 壮年 中年 长辈 老年
nchar("欢迎关注狗熊会")
[1] 7
nchar("hello world")
[1] 11
substr("欢迎光临狗熊会", 1, 4)
[1] "欢迎光临"
paste(c("双11","是个","什么节日"), collapse="-" )
[1] "双11-是个-什么节日"
paste(c("双11","是个","什么节日"), sep="-" )
[1] "双11" "是个" "什么节日"
paste("A", 1:4, collapse = "_")
[1] "A 1_A 2_A 3_A 4"
paste("A", 1:4, sep = "_", collapse = ":")
[1] "A_1:A_2:A_3:A_4"
txt = c("狗熊会","CluBear","双11","生日")
grep("Bear", txt)
[1] 2
gsub("生日","Happy Birthday",txt)
[1] "狗熊会" "CluBear" "双11" "Happy Birthday"
txt
[1] "狗熊会" "CluBear" "双11" "生日"
(zero = matrix(0, nrow = 3, ncol = 3, byrow = FALSE))
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
(dig = diag(rep(1,6)))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 0 0
[2,] 0 1 0 0 0 0
[3,] 0 0 1 0 0 0
[4,] 0 0 0 1 0 0
[5,] 0 0 0 0 1 0
[6,] 0 0 0 0 0 1
rep(1:4, 2)
[1] 1 2 3 4 1 2 3 4
rep(1:4, each=2)
[1] 1 1 2 2 3 3 4 4
rep(1:4, each = 2, length.out = 6)
[1] 1 1 2 2 3 3
rep(1:4, 3, each = 2)
[1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4
dim(zero)
[1] 3 3
zero[1,2]
[1] 0
colnames(zero) = paste0("x_", 1:3)
zero
x_1 x_2 x_3
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
(price = list(year2014=36:33, year2015=32:35, year2016=30:27))
laply(price, mean)
lapply(price, mean)
sapply(price, mean)
sapply(price, quantile)
(amount = list(year2014=rep(200,4), year2015=rep(100,4), year2016=rep(300,4)))
(income= mapply("*", price, amount))
library(plyr)
(test=read.table("weibo.txt", sep = "\t", fill = T, fileEncoding = "UTF-8", quote = ""))
test[92,]
weibo1=readLines("weibo.txt", encoding = "UTF-8")
head(weibo1)
tmp=strsplit(weibo1, "\t")
tmp[1:2]
tmplength =sapply(tmp, length)
table(tmplength)