
R
sixk
R与Python的初学者,边学边总结
展开
-
Python与R布尔值TRUE 和FALSE 的相互转换,Python R布尔值取反
Python与R布尔值TRUE 和FALSE 的相互转换,Python R布尔值取反原创 2022-05-08 12:32:13 · 6135 阅读 · 0 评论 -
R语言如何将路径中的\\转换为/
将\\转换为/这里用了原生字符可参考https://www.cnblogs.com/soldier-justice/p/15200277.html在R语言中,使用类似Python原生字符串的方式处理路径替换方法gsub("\\\\", "/", a)原创 2022-05-07 17:31:12 · 2504 阅读 · 3 评论 -
时间序列分析——基于R语言案例数据课后数据
去这个www.crup.com.cn网址搜,里面全都有。免费的。...原创 2021-06-22 17:29:18 · 7974 阅读 · 2 评论 -
R语言table转换为data.frame
在这里插入代码片原创 2021-06-15 10:06:33 · 16102 阅读 · 2 评论 -
R语言caret包preProcess()标准化出现错误: Matrices or data frames are required for preprocessing
原因数据是AsIs格式的,默认为一列了,关于AsIs参考如下博客https://blog.youkuaiyun.com/weixin_48172266/article/details/117747605?spm=1001.2014.3001.5502R语言里面“AsIs“到底是啥,用的时候咋处理例子,hdi包里的riboflavin数据集,其中riboflavin$x为“AsIs“格式数据。library(hdi) preProcValues = preProcess(riboflavin$x, metho原创 2021-06-09 16:57:55 · 4923 阅读 · 0 评论 -
R语言里面“AsIs“到底是啥,用的时候咋处理
例子用的是hdi包中的一个riboflavin数据集(核黄素)library(hdi)dim(riboflavin)这里我没用head(riboflavin),因为变量太多了。这里用的dim(),可以看出来这个数据集里面有71行2列,这里的两列分别是x和y。可以通过以下的代码看一下。riboflavin$xriboflavin$y可以看一下类型class(riboflavin$x)class(riboflavin$y)"AsIs"是啥看看大佬的解释吧,点击下面的链接然后进入原创 2021-06-09 16:49:05 · 6830 阅读 · 0 评论 -
R语言ggplot画条形图按照想要的顺序排列
加载所需要的包library(ggplot2)library(dplyr)创建数据集Year <- c(rep("2012" , 3) , rep("2013" , 3) , rep("2014" , 3) , rep("2015" , 3) )Legend <- rep(c("A" , "X" , "E") , 4)Count <- abs(rnorm(12 , 0 , 15))data <- data.frame(Year,Legend,Count)data原创 2021-06-03 23:52:42 · 32396 阅读 · 0 评论 -
ggplot中怎么使用类似par()的函数实现同时绘制多幅图
方法一library(ggplot2)library(patchwork)p1 <- ggplot(iris,aes(x=Sepal.Length))+geom_histogram(fill='#58a6a6')p2 <- ggplot(iris,aes(x=Sepal.Width))+geom_histogram(fill='#1c595a')p3 <- ggplot(iris,aes(x=Petal.Length))+geom_histogram(fill='#daae31'原创 2021-05-28 16:31:31 · 6336 阅读 · 1 评论 -
RStudio的help字体的调整大小
1.找到RStudio默认安装目录下的R.css文件C:\Program Files\RStudio\resources2.双击进入编辑一般就是直接进入记事本编辑了将红框那一块替换为body, td { /*font-family: Helvetica, sans-serif; */ font-family: 'Source Sans Pro', 'Lucida Grande', Verdana, Arial, sans-serif !important; font-si原创 2021-05-27 16:21:50 · 7959 阅读 · 2 评论 -
Rmarkdown输入空格
在这里将这个半角改成全角。也就是将上图月亮改成下图太阳。这种输入法状态下就可以输入空格并输出到html及其他格式。原创 2021-05-27 10:56:22 · 5426 阅读 · 0 评论 -
R语言backtest函数
Tsay, Ruey S. 2013. 金融数据分析导论:基于R语言. 机械工业出版社.原始版本:"backtest" <- function(m1, rt, orig, h, xre=NULL, fixed=NULL, inc.mean=TRUE){ # m1: is a time-series model object # orig: is the starting forecast origin # rt: the time series # xre: the indepen原创 2021-05-18 10:30:31 · 6721 阅读 · 0 评论 -
R语言求矩阵的秩
qr(data)$rank原创 2021-05-13 22:34:26 · 7191 阅读 · 0 评论 -
R语言输出为eps格式的图片,latex
library(grDevices)cairo_ps("C:\\Users\\10539\\Desktop\\whatever.eps")plot(data)title(main="n = 30,p = 8,σ = 4,ρ= 0.9", col.main="black") dev.off()原创 2021-05-13 22:32:49 · 7884 阅读 · 1 评论 -
R老版本的网址
https://cran.r-project.org/bin/windows/base/old原创 2021-05-07 16:01:25 · 5044 阅读 · 0 评论 -
R语言中有inf的数据怎么求平均
数据例如下面的数据data:col1 col2 col3A 2 2B 5 infC 4 3我们想用col2中的5代替inf代码mean(ifelse(data$col3 == Inf, data$col2, data$col3))原创 2021-05-06 21:55:24 · 5874 阅读 · 0 评论 -
R语言将数字类型转换为百分数
val <- c(0.12,0.23,2.33)scales::percent(val, 0.01)[1] “12.00%” “23.00%” “233.00%”原创 2021-05-06 20:30:04 · 13373 阅读 · 0 评论 -
R语言查看每列的缺失值
代码library(tidyverse)library(naniar)data %>% miss_var_summary()注%>%为管道函数,不懂可以看下面的链接管道函数miss_var_summary在naniar包里面原创 2021-05-06 20:14:16 · 8655 阅读 · 1 评论 -
R语言将字符串中的数字提取出来作为值
用到的函数extract()例子首先构造数据week <- c("week1","week2","week3","week4")value <- c(5,6,7,8)data <- data.frame(week,value)week value1 week1 52 week2 63 week3 74 week4 8extract(data,week,"week","(\\d)",convert = TRUE)#(\\d)表示任意原创 2021-05-06 20:05:39 · 16962 阅读 · 7 评论 -
R语言str_c函数
将多个字符串合并成一个字符串格式:str_c(…, sep = “”, collapse = NULL)三个参数的意思… 表示的是多个字符向量sep:要在输入向量之间插入的字符串collapse:用于将输入向量组合成单个字符串的可选字符串这翻译的都不像人话,直接看例子吧例子str_c("Letter: ", letters)[1] “Letter: a” “Letter: b” “Letter: c” “Letter: d” “Letter: e”[6] “Letter: f” “原创 2021-05-05 23:13:51 · 8814 阅读 · 1 评论 -
R语言统计列中每个字符串的长度
R语言统计列中每个字符串的长度library(tidyverse)str <- c("yuwen","shuxue","yingyu","zhengzhi","lishi","dili")str <- as_tibble(str)#转化为tibble才有列名str %>%mutate(strlen = str_length(value))A tibble: 6 x 2value strlen 1 yuwen 52 shuxue原创 2021-05-05 22:56:22 · 6073 阅读 · 0 评论 -
R语言everything()函数
这个函数在dplyr包里面这个函数主要是用来选出所有的变量。例子library(tidyverse)# For better printingiris <- as_tibble(iris)#Use everything() to select all variables:iris %>% select(everything())#> # A tibble: 150 x 5#> Sepal.Length Sepal.Width Petal.Length P原创 2021-05-05 22:44:00 · 3824 阅读 · 0 评论 -
Error in plot.new() : figure margins too large
画图区域太小了,把下图的区域拖的大一些就好了原创 2021-05-05 16:52:24 · 1863 阅读 · 0 评论 -
R语言rmarkdown中怎么添加交互图表
这个例子不好做,做了在这里展示的也只是个图片,而不是个交互的图表R语言交互式图表原始的网址:https://bookdown.org/xiao/RAnalysisBook/r-markdown.html#section-5.7原创 2021-05-04 20:52:59 · 3805 阅读 · 0 评论 -
R语言提取一列以“字符”开头的行
例如取出mtcars中的Merc车的几行mtcars[grep("Merc",rownames(mtcars)),]Merc 240D 24.4 4 146.7 62 3.69 3.19 20.0 1 0 4 2Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4Merc 280原创 2021-05-04 20:49:05 · 5936 阅读 · 0 评论 -
R语言求任意两个日期的天数
begin = as.Date("2021-03-15")end = as.Date("2021-05-04")difftime(end, begin, units = "weeks")difftime(end, begin, units = “weeks”)difftime(end, begin, units = "hours")Time difference of 1200 hours可以用这个函数看看自己的人生经过了多少年,各位要好好珍惜自己的时间。...原创 2021-05-04 20:36:17 · 5285 阅读 · 0 评论 -
R语言lubridate包(处理时间数据的专用包)
加载包# install.packages(lubridate)library(lubridate)第一个函数ymd():解析日期为年月日格式x <- c("09-01-01", "09-01-02", "09-01-03")ymd(x)[1] “2009-01-01” “2009-01-02” “2009-01-03”x <- c("2009-01-01", "2009-01-02", "2009-01-03")ymd(x)[1] “2009-01-01” “200原创 2021-05-04 20:27:58 · 4193 阅读 · 0 评论 -
R语言相关性分析及通过corrplot函数画出一些好看的相关关系图
数据集我们以内置的mtcars为例,至于里面的每个变量是啥意思,可以看下面的[, 1] mpg Miles/(US) gallon[, 2] cyl Number of cylinders[, 3] disp Displacement (cu.in.)[, 4] hp Gross horsepower[, 5] drat Rear axle ratio[, 6] wt Weight (1000 lbs)[, 7] qsec 1/4 mile time[, 8] vs Engine (0 =原创 2021-05-04 18:11:36 · 8487 阅读 · 0 评论 -
R语言怎么将“[3, 4, 5, 6]“转换为数字格式
x <- "[3, 4, 5, 6]"x <- gsub("\\[|\\]","", x)y <- as.numeric(unlist(strsplit(x, ",", fixed=TRUE)))y#> [1] 3 4 5 6原创 2021-05-01 22:48:03 · 2560 阅读 · 0 评论 -
R语言read.csv里面的参数,stringsAsFactors
简单说就是stringsAsFactors = FALSE,此时读取进来的数据框将不会默认把字符型转化为因子,具体这个有啥用处可以参考下面的博客。参考文档R语言数据框中的stringsAsFactors参数https://blog.youkuaiyun.com/weixin_43953722/article/details/103188934R语言数据框中的stringsAsFactors参数...原创 2021-04-28 15:18:51 · 5840 阅读 · 0 评论 -
R语言e1071包做SVR怎么取出权重
w = drop(t(svm_model$coefs)%*%as.matrix(X[svm_model$index,1:length(SFI)]))#取出超平面的权重向量svm_model为训练的模型1:length(SFI),表示的是一个向量,不用纠结SFI是啥,假设应该是求14个变量的权重,这里就是1:14,求8个变量的权重就是1:8,这个 SFI是我写的代码里命名的一个东西,其他的部分照抄。...原创 2021-04-27 20:46:42 · 2536 阅读 · 5 评论 -
R语言向量的集合运算
向量的交集用intersect(x, y)并集用union(x, y)差集用setdiff(x, y)集合是否相等,用setequal(x, y),确定元素x是否是集合y的某个元素用is.element(x, y),它等价于x %in% y.(这里x可以是一个元素,也可以是一个变量)例子交集x <-c(1,2,3) y <- c(2,3,4)intersect(x, y)intersect(x, y)[1] 2 3并集x <-c(1,2,3) y <原创 2021-04-27 15:24:48 · 2855 阅读 · 0 评论 -
Error in install.packages : Updating loaded packages WARNING: Rtools is required to build R packages
提示信息Show in New WindowError in install.packages : Updating loaded packagesWARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:https://cran.rstud原创 2021-04-27 14:29:55 · 13814 阅读 · 2 评论 -
Package LibPath Version Priority Depends Imports LinkingTo Suggests Enhances License License_i
解决措施:将installed.packages(“DT”)换成install.packages(“DT”)原创 2021-04-27 14:22:50 · 2658 阅读 · 0 评论 -
R语言怎么获取行数
R语言获取数据框行数的方法1)dim(dataframe)[1]2)nrow(dataframe)3)length(dataframe[,1])原创 2021-04-19 16:04:58 · 27803 阅读 · 1 评论 -
如何找到并安装在Rstudio中找不到的包(简单方法)
以ncvreg为例打开R,不是Rstudioinstall.packages(“ncvreg”)右边那个窗口时选包的来源,选那个Lanzhou那个然后就会显示安装成功了但是吧,还是有些问题,在Rstudio种用??ncvreg还是找不到这个包的官方文档。怎么找官方文档可以参考一下博客找官方文档...原创 2021-04-17 17:34:41 · 6544 阅读 · 0 评论 -
如何找到并安装在Rstudio找不到的包
以ncvreg为例子在Rstudio里面用??ncvreg可以查找这个包,发现没有去R的网站里面找这个包网站地址http://cloud.r-project.org/R包官方网址找到左边的Packages,点进去这里给出来了两种包的排序方式,一种是按日期,一种是按照name,点进去那个按name排序的点进来以后是按照name首字母排序的,然后点n,找到这个包(或者是用键盘同时按住ctrl+f两个键,调出搜索框,搜索框在屏幕右上角,然后输入ncvreg,就找到了)用搜索框搜索点进去原创 2021-04-17 17:25:35 · 11606 阅读 · 4 评论 -
将图例放在最下面并且横向放置(ggplot2:数据分析与图形艺术6.4.4练习题第3题)
library(ggplot2)ggplot(mpg,aes(displ,hwy,color=class))+ geom_point()+ geom_smooth(method = "lm")+ theme(legend.position = "bottom" ,legend.box = "horizontal")+ scale_shape(guide = guide_legend(title.position = "top")) + scale_colour_discrete(gui原创 2021-04-10 21:04:54 · 8420 阅读 · 0 评论 -
R语标注数据离群点(ggplot2:数据分析与图形艺术5.3节数据)
书里代码少了一行,这里修改过了library(ggplot2)library(tidyverse)mod <- loess(hwy~displ,data=mpg)grid <- data.frame(displ=seq(min(mpg$displ),max(mpg$displ),length=50))grid$hwy <- predict(mod,newdata = grid)std_resid <- resid(mod)/mod$smpg <- mutate(m原创 2021-04-10 12:10:14 · 3264 阅读 · 0 评论 -
R语言filter()函数
filte():仅能筛选观测filte()第一个参数是数据框,后面的是逻辑值x==yx !=y (x和y不等)x %in% c(“a”,“b”,“c”)(x属于右侧)x>y,x>=y,x<y,x<=y也可以用逻辑运算符组合起来!xx&yX|yxor(x,y) (异或)例子library(dplyr)head(iris)dplyr::filter(iris,Sepal.Length>7)#筛选花萼长度>7的观测还有就是我自己写的文章里原创 2021-04-09 23:50:39 · 29253 阅读 · 2 评论 -
R语言select()filter()subset()筛选函数
filte():仅能筛选观测filte()第一个参数是数据框,后面的是逻辑值x==yx !=y (x和y不等)x %in% c(“a”,“b”,“c”)(x属于右侧)x>y,x>=y,x<y,x<=y也可以用逻辑运算符组合起来!xx&yX|yxor(x,y) (异或)library(dplyr)head(iris)dplyr::filter(iris,Sepal.Length>7)#筛选花萼长度>7的观测select():仅能筛选变量原创 2021-04-09 23:48:38 · 22103 阅读 · 0 评论