![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
R
向爱我
这个作者很懒,什么都没留下…
展开
-
概率统计与模拟
The Normal Distributiondnorm(0, mean = 0, sd = 1, log = FALSE)pnorm(0, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)qnorm(0.05, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)rnorm(10, mean = 0, sd = 1)Descriptiondnorm gives the density.原创 2021-06-04 11:24:21 · 462 阅读 · 0 评论 -
一些好操作
*names*显示数据集中各变量的名字*n_distinct*向量中仅出现一次的元素的个数*unique*向量中仅出现一次的元素所形成的向量*sample*抽样,如:sample(1:3,10,replace = FALSE) #无放回抽10个,默认为无放回sample(data$ID,5,replace = TRUE) #有放回从数据集里抽5个sample(c("a","b","c"),10,replace = TRUE,prob = c(0.1,0.5,0.4)) #概率抽样.原创 2021-06-03 21:32:57 · 128 阅读 · 0 评论 -
若干题目
1.(1)请首先剔除数据中实际出发时间(dep_time)缺失的观测值,然后生成一个新的变量dep_interval,用来将数据中的实际出发时间分为上午(6:01-12:00)和下午(12:01-18:00),晚上(18:01-24:00)和凌晨(0:01-6:00)四组,将每一年每一月每一天内每个dep_interval分组(year,month,day,dep_interval),计算每一组的平均到时延误时间和平均到达的机场数量以及到达延误的方差。alter_flights <- flig.原创 2021-06-01 09:52:30 · 414 阅读 · 0 评论 -
Modeling
OLS regression##basicslm(y~x,sim1) #make a regression with y towards x from dataset `sim1``add_predictions()` adds predictions`add_residuals()` adds residuals#for examplesim1_mod <- lm(y~x,sim1)sim1 %>% data_grid(x) %>% #`data_grid(原创 2021-05-21 11:24:40 · 117 阅读 · 0 评论 -
Vector
Type Atomic Vector logicalintegerdoublecharacter#to name the vector> c(x=1,y=4,"father"=8) x y father 1 4 8 List #use `str()` to show the exact content of a list> list(1:2,5,"a")[[1]][1] 1 2[[2]]原创 2021-05-20 21:11:38 · 58 阅读 · 0 评论 -
Creating Functions and Iterations
Basicsrescale01 <- function(x) { rng <- range(x, na.rm = TRUE) (x - rng[1]) / (rng[2] - rng[1])}#name, argument and body are needed for a function#the last expression is the value that returns by default;use `return()` to specially return原创 2021-05-19 22:27:06 · 85 阅读 · 0 评论 -
Five Major Functions for Data Transformation
filterfilter(flights, month == 1, day == 1)filter(flights, month %in% c(11, 12))#a way to check the number of NAflights %>% mutate(temp = is.na(dep_time) ) %>% select(temp) %>% table()arrangearrange(flights,arr_delay) #from s原创 2021-05-19 21:30:53 · 81 阅读 · 0 评论 -
Data Tidying
Pivot It Longer or Widertable4atable4a %>% pivot_longer( c("1999", "2000"), names_to = "year", values_to = "cases")#gather the columns as given and set the original names and values under variables of new namestable2table2 %>% pivot_wider原创 2021-05-12 18:54:12 · 104 阅读 · 0 评论 -
Data Parsing
Creating A Matrixtibble( x = 1:5, y = 1, z = x ^ 2 + y)# A tibble: 5 x 3# x y z# <int> <dbl> <dbl># 1 1 1 2# 2 2 1 5# 3 3 1 10# 4 4 1 17# 5 5 1原创 2021-05-12 09:45:37 · 161 阅读 · 0 评论 -
Drawing Plots
Orders1.draw the plot1.1 basic#draw a bar plot,for categorical variables:ggplot(diamonds) + geom_bar(aes(cut))#for continuous variables:ggplot(diamonds) + geom_histogram(aes(carat), binwidth = 0.2)#this can be used to find the clusters #原创 2021-05-11 20:07:54 · 243 阅读 · 0 评论 -
Factor
Creating a factorFix the order and spelling of a string:x1 <- c("Dec", "Apr", "Jan", "Mar")month_levels <- c( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")y1 <- factor(x1, levels = month_levels)s原创 2021-05-07 10:44:42 · 122 阅读 · 0 评论 -
Regular Expression
基本操作1.str_length() 看长度(含空格)2.str_c() 组合例:str_c("x","y",sep=",") 得到x,ystr_c(c("x","y"),collapse=",") 得到x,y3.str_to_upper() str_to_lower() 变大写小写4. str_sub() 截取例:x <- c("apple")str_sub(x,1,3) 得appstr_sub(x,-3,-1) 得ple #负号表示从后往前数的位置匹配str_原创 2021-05-05 12:25:08 · 115 阅读 · 0 评论 -
Joining Dataframes
键一个变量存在于两个表中,并可唯一标识其中一个表。相对于可唯一标识的表,该变量称主键;相对于另一个表,该变量称外键。人为添加的主键称代理键。如:flights %>% mutate(iden=row_number())#iden就是代理键,将每个观测值依次标为1,2,3...合并连接1.连接方式内连接(只保留相同) ,左连接(保留左侧),右连接(保留右侧),全连接(两侧都保留)#无对应部分用NA代替#重复部分依次对应连接格式均为:inner_join(x,y,by="ke原创 2021-04-23 10:45:22 · 95 阅读 · 0 评论