-
R语言学习笔记之str函数
2018-01-11 16:37:16str函数 即structure,紧凑的显示对象内部结构,即对象里有什么。 例如:当我们head数据的时候,若某列内容太多,则不会显示出来,而...当使用head函数时,显示内容如下: 而使用了str函数之后,显示如下所示:str函数
即structure,紧凑的显示对象内部结构,即对象里有什么。
例如:当我们head数据的时候,若某列内容太多,则不会显示出来,而用str函数,便可在窗口中逐行显示数据中列的内容。
如下图,读取数据如下:
当使用head函数时,显示内容如下:
而使用了str函数之后,显示如下所示:
-
R语言 | 自定义函数对数据集(data.frame)的列进行条件判断计算
2018-12-02 16:05:121.使用iris数据集 > iris_10 <- head(iris, n = 10) ## 自定义函数:如果x >= 5.0, z = y *10 > get_With_function <- function(x, y, z){ + if(x >= 5.0){ + z <...1.使用iris数据集
> iris_10 <- head(iris, n = 10) ## 自定义函数:如果x >= 5.0, z = y *10 > get_With_function <- function(x, y, z){ + if(x >= 5.0){ + z <- y * 10 + } + c(zlie = z ) + }
2.保险起见,设定z列为0,可能也不需要
> iris_10$z <- 0
3.运用自定义函数,对data.frame的x行进行判断,对y列进行运算,赋值到z列
4…注意Map的使用
> iris_10$z <- with( + iris_10, + Map( + get_With_function, + iris_10$Sepal.Length, + iris_10$Sepal.Width, + z + ) + ) > iris_10 Sepal.Length Sepal.Width Petal.Length Petal.Width 1 5.1 3.5 1.4 0.2 2 4.9 3.0 1.4 0.2 3 4.7 3.2 1.3 0.2 4 4.6 3.1 1.5 0.2 5 5.0 3.6 1.4 0.2 6 5.4 3.9 1.7 0.4 7 4.6 3.4 1.4 0.3 8 5.0 3.4 1.5 0.2 9 4.4 2.9 1.4 0.2 10 4.9 3.1 1.5 0.1 Species z 1 setosa 35 2 setosa 0 3 setosa 0 4 setosa 0 5 setosa 36 6 setosa 39 7 setosa 0 8 setosa 34 9 setosa 0 10 setosa 0
-
R语言中如何退出程序 类似python的sys.exit函数?
2020-05-18 18:50:03R中使用return(message("")) 例子: tt = function(dat){ # 判断如果数据的列数为3列,就退出,给出提示,如果是其它列,打印数据前六行。 if(dim(dat)[2] == 3){ return(message("错误,数据是三列")) }else{ ...R中使用return(message(""))
例子:
tt = function(dat){ # 判断如果数据的列数为3列,就退出,给出提示,如果是其它列,打印数据前六行。 if(dim(dat)[2] == 3){ return(message("错误,数据是三列")) }else{ head(dat) } } t1 = data.frame(ID=1:10,y1=rnorm(10),y2=rnorm(10)) t2 = data.frame(ID=1:10,y1=rnorm(10),y2=rnorm(10),y3=rnorm(10)) tt(t1) tt(t2)
结果:
> tt(t1) 错误,数据是三列 > tt(t2) ID y1 y2 y3 1 1 1.025571370 0.3317820 -0.6002596 2 2 -0.284773007 1.0968390 2.1873330 3 3 -1.220717712 0.4351815 1.5326106 4 4 0.181303480 -0.3259316 -0.2357004 5 5 -0.138891362 1.1488076 -1.0264209 6 6 0.005764186 0.9935039 -0.7104066
可以看出:
- 数据为三列时,程序退出,给出提示
- 数据为其它列时,程序运行,打印前六行
-
r语言直方图_R语言可视化(四):频率直方图绘制
2020-12-08 07:54:5404.频率直方图绘制清除当前环境中的变量rm(list=ls())设置工作目录setwd("C:/Users/Dell/Desktop/R_Plots/04histogram/")hist函数绘制频率直方图# 使用内置mtcars数据集head(mtcars)## mpg cyl disp hp drat wt qsec...04.频率直方图绘制
清除当前环境中的变量
rm(list=ls())
设置工作目录
setwd("C:/Users/Dell/Desktop/R_Plots/04histogram/")
hist函数绘制频率直方图
# 使用内置mtcars数据集
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
head(mtcars$mpg)
## [1] 21.0 21.0 22.8 21.4 18.7 18.1
# 基础hist函数绘制频率直方图
hist(mtcars$mpg)hist(mtcars$mpg, breaks = 10, col = "red",
xlab = "Miles per Gallon")hist(mtcars$mpg, breaks = 10, col = "blue",
freq = F, # 表示不按照频数绘图
xlab = "Miles per Gallon")
# 添加密度曲线
lines(density(mtcars$mpg),col= "red",lwd=2)
# 添加轴须线
rug(jitter(mtcars$mpg))ggplot2包绘制直方图
library(ggplot2)
# 读取示例数据
data "demo_histgram.txt")
names(data) "length"
head(data)
## length
## 1 62
## 2 134
## 3 290
## 4 316
## 5 98
## 6 129
ggplot(data,aes(length,..density..)) + xlim(c(0,1000)) +
geom_histogram(binwidth = 2, fill="red") +
xlab("Insertion Size (bp)") +
theme_bw()# 使用diamonds内置数据集
head(diamonds)
## # A tibble: 6 x 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
ggplot(diamonds, aes(carat)) +
geom_histogram()# 设置bin的数目
ggplot(diamonds, aes(carat)) +
geom_histogram(bins = 200)# 设置bin的宽度
ggplot(diamonds, aes(carat)) +
geom_histogram(binwidth = 0.05)# 添加填充色
ggplot(diamonds, aes(price, fill = cut)) +
geom_histogram(binwidth = 500)# You can specify a function for calculating binwidth, which is
# particularly useful when faceting along variables with
# different ranges because the function will be called once per facet
mtlong ## No id variables; using all as measure variables
head(mtlong)
## variable value
## 1 mpg 21.0
## 2 mpg 21.0
## 3 mpg 22.8
## 4 mpg 21.4
## 5 mpg 18.7
## 6 mpg 18.1
ggplot(mtlong, aes(value, fill=variable)) + facet_wrap(~variable, scales = 'free_x') +
geom_histogram(binwidth = function(x) 2 * IQR(x) / (length(x)^(1/3)))ggpubr包绘制直方图
library(ggpubr)
# Create some data format
set.seed(1234)
wdata = data.frame(
sex = factor(rep(c("F", "M"), each=200)),
weight = c(rnorm(200, 55), rnorm(200, 58)))
head(wdata)
## sex weight
## 1 F 53.79293
## 2 F 55.27743
## 3 F 56.08444
## 4 F 52.65430
## 5 F 55.42912
## 6 F 55.50606
# Basic density plot
# Add mean line and marginal rug
gghistogram(wdata, x = "weight",
fill = "lightgray", # 设置填充色
add = "mean", # 添加均值线
rug = TRUE # 添加轴须线
)# Change outline and fill colors by groups ("sex")
# Use custom color palette
gghistogram(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800") # 设置画板颜色
)# Combine histogram and density plots
gghistogram(wdata, x = "weight",
add = "mean", rug = TRUE,
fill = "sex", palette = c("#00AFBB", "#E7B800"),
add_density = TRUE # 添加密度曲线
)sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggpubr_0.2.1 magrittr_1.5 ggplot2_3.2.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.1 plyr_1.8.4 pillar_1.4.2
## [4] compiler_3.6.0 tools_3.6.0 zeallot_0.1.0
## [7] digest_0.6.20 viridisLite_0.3.0 evaluate_0.14
## [10] tibble_2.1.3 gtable_0.3.0 pkgconfig_2.0.2
## [13] rlang_0.4.0 cli_1.1.0 yaml_2.2.0
## [16] xfun_0.8 withr_2.1.2 dplyr_0.8.3
## [19] stringr_1.4.0 knitr_1.23 vctrs_0.2.0
## [22] grid_3.6.0 tidyselect_0.2.5 glue_1.3.1
## [25] R6_2.4.0 fansi_0.4.0 rmarkdown_1.13
## [28] reshape2_1.4.3 purrr_0.3.2 scales_1.0.0
## [31] backports_1.1.4 htmltools_0.3.6 assertthat_0.2.1
## [34] colorspace_1.4-1 ggsignif_0.5.0 labeling_0.3
## [37] utf8_1.1.4 stringi_1.4.3 lazyeval_0.2.2
## [40] munsell_0.5.0 crayon_1.3.4END
▼更多精彩推荐,请关注我们▼
把时间交给阅读
您点的每个赞,我都认真当成了喜欢
-
java读取大excel 提前判断_R语言读取Excel、R与本机文件
2021-01-06 10:37:40每类文件都会使用对应的分隔符,例如用逗号分隔的文件为.csv文件,读取方式为:x 2.head(x) tail(x) 显示x对象首尾6行的内容(默认为6行)head(x,n=10),一次显示10行内容3.read.table()函数在使用时,若是.csv文件... -
R语言——assignment2
2018-11-17 11:42:25读取forclass.csv > data<-read.csv(file.choose()) #选择forclass.csv文件,读入数据 ... head(data) #数据过多,这里使用head()函数展示前六行 weight Time Chick Diet 1 42 0 1 1 2 51 2 1 1 3 5... -
直方图设置坐标_R语言直方图绘制ggplot2
2021-01-09 15:06:23R语言直方图绘制ggplot2素娥2020/10/4本文我们要用iris数据集进行直方图的绘制本文主要使用ggplot2包中的ggplot()+geom_histgram()函数进行绘制图片首先还是观察数据类型data<-irislibrary(knitr)tt<-head... -
R语言笔记-用基本包处理数据框
2020-10-18 00:04:09查看数据框内容 函数 解释 head() 显示数据集前几行 tail() 显示数据集后几行 str() 数据集概览 ...直接输入数据框名称:等价于使用print()函数 对于比较大的数据框,会占用很多屏幕 head(som -
echarts树图节点背景颜色_R语言中的聚类树图绘制
2020-11-25 12:39:20清除当前环境中的变量rm(list=ls())使用dendrogram函数绘制聚类树图# 查看内置示例数据head(USArrests)## Murder Assault UrbanPop Rape## Alabama 13.2 236 58 21.2## Alaska 10.0 263 48 44.5#... -
echarts树图节点背景颜色_R语言可视化(二十九):聚类树图绘制
2020-11-25 12:39:2229. 聚类树图绘制清除当前环境中的变量rm(list=ls())设置工作目录setwd("C:/Users/Dell/Desktop/R_Plots/29dendrogram/")使用dendrogram函数绘制聚类树图# 查看内置示例数据head(USArrests)## Murder Assault ... -
r语言 load Rdata 获取表名 并直接转为数据表
2017-03-27 14:29:00首先指定 load结果为一个对象 然后此对象的值 即为 str的 数据表名 然后使用eval(parse(text = l)) 两个函数 将字符串 转可执行对象 即可完成重新赋值 > l <- load("D:\\work\\task\\task_data\\02_12306\\... -
外部函数接口LibFFI.zip
2019-07-16 07:42:58\r\n \r\n \r\n \r\n \r\n \u8f6f\u4ef6\u9996\u9875\r\n \u8f6f\u4ef6\u4e0b\u8f7d\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\nwindow.changyan.api.config({\r\nappid:... -
Go基础之标准库
2018-09-07 13:18:56go作为一门面向服务,面向并发的语言,其http库是一个很重要的库. 使用http客户端发送请求 使用http.Client控制请求头部等 ...Get、Head、Post和PostForm函数发出HTTP/ HTTPS请求。 r... -
Windows 程序设计(第5版)(上、下册)--详细书签版
2012-04-22 18:40:049.6.7 windows的head程序 第十章 菜单及其他资源 10.l 图标、光标、字符串和定制资源 10.1.1 将图标添加到程序 10.1.2 获取图标句柄 10.1.3 在程序中使用图标 10.1.4 使用自定义光标 10.1.5 ... -
Windows 程序设计(第5版)(上、下册)--源代码
2012-04-22 19:21:459.6.7 windows的head程序 第十章 菜单及其他资源 10.l 图标、光标、字符串和定制资源 10.1.1 将图标添加到程序 10.1.2 获取图标句柄 10.1.3 在程序中使用图标 10.1.4 使用自定义光标 10.1.5 ... -
《数据结构 1800题》
2012-12-27 16:52:03《数据结构 1800题》 第一章 绪论 一、选择题 1. 算法的计算量的大小称为计算的(B )。【北京邮电大学2000 二、3 (20/8分)】 A.效率 B....2. 算法的时间复杂度取决...10. 若将数据结构定义为一个二元组(D,R),... -
有没有大神可以帮我写一下删除成绩
2020-06-18 13:13:47void search()//主函数中的子函数调用了两个子函数,使用 switch 语句实现 { int chioce; system("cls"); printf("\t\t1-----姓名查询信息-----\n"); printf("\n\t\t2-----学号查询信息-----\n"); printf... -
帮忙加一个删除成绩的步骤
2020-06-18 13:19:08void search()//主函数中的子函数调用了两个子函数,使用 switch 语句实现 { int chioce; system("cls"); printf("\t\t1-----姓名查询信息-----\n"); printf("\n\t\t2-----学号查询信息-----\n"); printf... -
PHP基础教程 是一个比较有价值的PHP新手教程!
2010-04-24 18:52:44所有变量都是局部变量,为了使得定义的函数中可以使用外部变量,使用global语句。而你要将该变量的作用范围限制在该函数之内,使用static语句。 $g_var = 1 ; // 全局范围 function test() { global $g_var; // 这样... -
asp.net知识库
2015-06-18 08:45:45在 SQL Server 2005 中使用表值函数来实现空间数据库 SQL Server 2005的30个最重要特点 同时安装sql2000和sql2005的经验 类如何与界面绑定 在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立... -
xscan
2011-11-04 17:50:27用"HEAD"替换"GET" 2.用"POST"替换"GET" 3.用"GET / HTTP/1.0\r\nHeader:" 替换 "GET" 4.用"GET /[filename]?param=" 替换 "GET"(可通过\dat\config.ini文件的"CGI-ENCODE\encode4_index_file"项设置[filename]) ... -
数据结构(C++)有关练习题
2008-01-02 11:27:18内容及步骤: 1、 在前一个实验的基础上,继续增加搜索函数Search(int Info)(如果找到结点,返回指向该结点的指针,如果没有,则返回空指针)和删除函数bool Delete(int Info),如果找到结点,则删除该结点,并... -
Oracle Database 11g数据库管理艺术--详细书签版
2012-09-30 01:09:453.6.2 用head和tail命令移动 47 3.7 文本的提取和排序 48 3.7.1 使用grep匹配模式 48 3.7.2 剪切、粘贴和联结文本 49 3.8 shell脚本 50 3.8.1 shell程序介绍 51 3.8.2 使用shell变量 51 3.8.3 用... -
linux内核 0.11版本源码 带中文注释
2009-08-31 15:10:21因此就不能有函数调用 - 这意味着fork 也要使用内嵌的代码,否则我们在从fork()退出 * 时就要使用堆栈了。 * 实际上只有pause 和fork 需要使用内嵌方式,以保证从main()中不会弄乱堆栈,但是我们同时还 * 定义了... -
计算机二级公共基础知识
2011-04-30 14:00:09线性单链表中,HEAD称为头指针,HEAD=NULL(或0)称为空表。 如果是双项链表的两指针:左指针(Llink)指向前件结点,右指针(Rlink)指向后件结点。 线性链表的基本运算:查找、插入、删除。 (2)带链的栈 栈也是... -
XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解
2008-12-05 08:39:07一个DTD文档包含:元素的定义规则,元素间关系的定义规则,元素可使用的属性,可使用的实体或符号规则。 DTD文件也是一个ASCII的文本文件,后缀名为.dtd。例如:myfile.dtd。 为什么要用DTD文件呢?我的理解是它... -
《Node.js 实战:使用 Egg.js + Vue.js + Docker 构建渐进式、可持续集成与交付应用》 陈微明 著 1.14 C/C++ (C/C++ 非常重要且复杂,由于时间关系,无限期待更) 《C 程序设计语言(第2版·新版)》(K&R...
-
导师计划--数据结构和算法系列(上)
2020-12-09 04:46:22<code>javascript语言辅助。本篇文章为上章,涉及的内容是基本的数据结构。在日本,晚上没事安排@…@,时间还是充足的...,于是自己整理下本系列知识点的上章内容。 <p><img alt=... -
java基础入门教程
2009-04-29 21:36:10如 Mi-croSoft、 IBM、 Netscape、 Novell、 Apple、 DEC、 SGI 等,因 此 ,Java 语言 被 美 国 的 著 名 杂 志 PC Magazine 评 为 1995年 十 大 优 秀科 技 产 品,(计 算 机 类 就 此 一项 入 选 ),随 之 大 量 出 ...
收藏数
30
精华内容
12
-
使用vue搭建微信H5公众号项目
-
6.1.共享内存界面设计
-
OpenGL学习笔记(二十六)
-
华为1+X——网络系统建设与运维(高级)
-
报错!!!MySQL在安装过程中的报错
-
vue3.0下如何挂载全局方法
-
【布道者】Linux极速入门
-
个人机器学习(Machine Learning, ML)笔记
-
鸿蒙系统Harmonyos源码架构分析-第1期第2课
-
包头鱼501-1000.7z
-
MySQL 高可用(DRBD + heartbeat)
-
10. 打折.cpp
-
双链表的创建插入删除带注释
-
基础语法——09常量及变量的命名规范
-
access应用的3个开发实例
-
MySQL NDB Cluster 负载均衡和高可用集群
-
项目管理工具与方法
-
基础语法——08变量类型及作用域
-
Docker 面试必问
-
一天学完MySQL数据库