我们用一个R内置的测试数据airquality举例什么是:

head(airquality)

ozone solar.r wind temp month day

1    41     190  7.4   67     5   1

2    36     118  8.0   72     5   2

3    12     149 12.6   74     5   3

4    18     313 11.5   62     5   4

5    NA      NA 14.3   56     5   5

6    28      NA 14.9   66     5   6

str(airquality)

'data.frame': 153 obs. of 6 variables:

$ ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...

$ solar.r: int  190 118 149 313 NA NA 299 99 19 194 ...

$ wind  : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...

$ temp  : int  67 72 74 62 56 66 65 59 61 69 ...

$ month  : int  5 5 5 5 5 5 5 5 5 5 ...

$ day    : int  1 2 3 4 5 6 7 8 9 10 ...

长数据:

"ozone" "solar.r" "wind" "temp" "month" "day"都是airquality的变量variable 名称,value值就是对应每个检测的值,这样的数据非常适合数据可视化。

head(melt(airquality), n = 10)

No id variables; using all as measure variables

variable value

1     ozone    41

2     ozone    36

3     ozone    12

4     ozone    18

5     ozone    NA

6     ozone    28

7     ozone    23

8     ozone    19

9     ozone     8

10    ozone    NA

宽数据:

宽数据通常是变量为列,检测为行所组成的数据框Data frame

head(airquality, n =10)

ozone solar.r wind temp month day

1     41     190  7.4   67     5   1

2     36     118  8.0   72     5   2

3     12     149 12.6   74     5   3

4     18     313 11.5   62     5   4

5     NA      NA 14.3   56     5   5

6     28      NA 14.9   66     5   6

7     23     299  8.6   65     5   7

8     19      99 13.8   59     5   8

9      8      19 20.1   61     5   9

10    NA     194  8.6   69     5  10

# 1.工作目录

setwd("reshape2")

# 2.安装和导入

# install.packages("reshape2")

library(reshape2)

# 3.功能测试

help(package="reshape2")

### 3.1 acast(),Cast functions Cast a molten data frame into an array or data frame.

str(acast)

# function (data, formula, fun.aggregate = NULL, ..., margins = NULL, subset = NULL,

# fill = NULL, drop = TRUE, value.var = guess_value(data))

# Cast functions Cast a molten data frame into an array or data frame.

names(airquality) <- tolower(names(airquality))

head(airquality)

# ozone solar.r wind temp month day

# 1    41    190  7.4  67    5  1

# 2    36    118  8.0  72    5  2

# 3    12    149 12.6  74    5  3

# 4    18    313 11.5  62    5  4

# 5    NA      NA 14.3  56    5  5

# 6    28      NA 14.9  66    5  6

head(acast(aqm, day ~ month ~ variable))

, , ozone

5  6   7  8  9

1 41 NA 135 39 96

2 36 NA  49  9 78

3 12 NA  32 16 73

4 18 NA  NA 78 91

5 NA NA  64 35 47

6 28 NA  40 66 32

, , solar.r

5   6   7  8   9

1 190 286 269 83 167

2 118 287 248 24 197

3 149 242 236 77 183

4 313 186 101 NA 189

5  NA 220 175 NA  95

6  NA 264 314 NA  92

, , wind

5    6    7    8    9

1  7.4  8.6  4.1  6.9  6.9

2  8.0  9.7  9.2 13.8  5.1

3 12.6 16.1  9.2  7.4  2.8

4 11.5  9.2 10.9  6.9  4.6

5 14.3  8.6  4.6  7.4  7.4

6 14.9 14.3 10.9  4.6 15.5

, , temp

5  6  7  8  9

1 67 78 84 81 91

2 72 74 85 81 92

3 74 67 81 82 93

4 62 84 84 86 93

5 56 85 83 85 87

6 66 79 83 87 84

acast(aqm, month ~ variable, mean)

# ozone  solar.r      wind    temp

# 5 23.61538 181.2963 11.622581 65.54839

# 6 29.44444 190.1667 10.266667 79.10000

# 7 59.11538 216.4839  8.941935 83.90323

# 8 59.96154 171.8571  8.793548 83.96774

# 9 31.44828 167.4333 10.180000 76.90000

acast(aqm, month ~ variable, mean, margins = TRUE)

# ozone  solar.r      wind    temp    (all)

# 5    23.61538 181.2963 11.622581 65.54839 68.70696

# 6    29.44444 190.1667 10.266667 79.10000 87.38384

# 7    59.11538 216.4839  8.941935 83.90323 93.49748

# 8    59.96154 171.8571  8.793548 83.96774 79.71207

# 9    31.44828 167.4333 10.180000 76.90000 71.82689

# (all) 42.12931 185.9315  9.957516 77.88235 80.05722

dcast(aqm, month ~ variable, mean, margins = c("month", "variable"))

# month    ozone  solar.r      wind    temp    (all)

# 1    5 23.61538 181.2963 11.622581 65.54839 68.70696

# 2    6 29.44444 190.1667 10.266667 79.10000 87.38384

# 3    7 59.11538 216.4839  8.941935 83.90323 93.49748

# 4    8 59.96154 171.8571  8.793548 83.96774 79.71207

# 5    9 31.44828 167.4333 10.180000 76.90000 71.82689

# 6 (all) 42.12931 185.9315  9.957516 77.88235 80.05722

### 3.2  melt( ),宽数据转化为长数据,Convert an object into a molten data frame.

aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)

head(aqm)

# month day variable value

# 1    5  1    ozone    41

# 2    5  2    ozone    36

# 3    5  3    ozone    12

# 4    5  4    ozone    18

# 6    5  6    ozone    28

# 7    5  7    ozone    23

### 3.3 colsplit()

?colsplit

# Split a vector into multiple columns

x <- c("a_1_T", "a_2_F", "b_2_T", "c_3_F")

vars <- colsplit(x, "_", c("trt", "time", "Boolean_value"))

vars

# trt time Boolean_value

# 1  a    1          TRUE

# 2  a    2        FALSE

# 3  b    2          TRUE

# 4  c    3        FALSE

str(vars)

# 'data.frame': 4 obs. of  3 variables:

#  $ trt          : chr  "a" "a" "b" "c"

# $ time        : int  1 2 2 3

# $ Boolean_value: logi  TRUE FALSE TRUE FALSE

### 3.4 recast(),Recast: melt and cast in a single step

### Recast: melt and cast in a single step

?recast

recast(french_fries, time ~ variable, id.var = 1:4)

# Aggregation function missing: defaulting to length

# time potato buttery grassy rancid painty

# 1    1    72      72    72    72    72

# 2    2    72      72    72    72    72

# 3    3    72      72    72    72    72

# 4    4    72      72    72    72    72

# 5    5    72      72    72    72    72

# 6    6    72      72    72    72    72

# 7    7    72      72    72    72    72

# 8    8    72      72    72    72    72

# 9    9    60      60    60    60    60

# 10  10    60      60    60    60    60

### 3.5 reshape2: built-in data

str(tips)

# 'data.frame': 244 obs. of  7 variables:

#  $ total_bill: num  17 10.3 21 23.7 24.6 ...

# $ tip      : num  1.01 1.66 3.5 3.31 3.61 4.71 2 3.12 1.96 3.23 ...

# $ sex      : Factor w/ 2 levels "Female","Male": 1 2 2 2 1 2 2 2 2 2 ...

# $ smoker    : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...

# $ day      : Factor w/ 4 levels "Fri","Sat","Sun",..: 3 3 3 3 3 3 3 3 3 3 ...

# $ time      : Factor w/ 2 levels "Dinner","Lunch": 1 1 1 1 1 1 1 1 1 1 ...

# $ size      : int  2 3 3 2 4 4 2 4 2 2 ...

# In all he recorded 244 tips. The data was reported in a collection of case studies for business statistics (Bryant & Smith 1995).

str(smiths)

# 'data.frame': 2 obs. of  5 variables:

#  $ subject: Factor w/ 2 levels "John Smith","Mary Smith": 1 2

# $ time  : int  1 1

# $ age    : num  33 NA

# $ weight : num  90 NA

# $ height : num  1.87 1.54

# A small demo dataset describing John and Mary Smith. Used in the introductory vignette.

str(french_fries)

# 'data.frame': 696 obs. of  9 variables:

#  $ time    : Factor w/ 10 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...

# $ treatment: Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...

# $ subject  : Factor w/ 12 levels "3","10","15",..: 1 1 2 2 3 3 4 4 5 5 ...

# $ rep      : num  1 2 1 2 1 2 1 2 1 2 ...

# $ potato  : num  2.9 14 11 9.9 1.2 8.8 9 8.2 7 13 ...

# $ buttery  : num  0 0 6.4 5.9 0.1 3 2.6 4.4 3.2 0 ...

# $ grassy  : num  0 0 0 2.9 0 3.6 0.4 0.3 0 3.1 ...

# $ rancid  : num  0 1.1 0 2.2 1.1 1.5 0.1 1.4 4.9 4.3 ...

# $ painty  : num  5.5 0 0 0 5.1 2.3 0.2 4 3.2 10.3 ...

# This data was collected from a sensory experiment conducted at Iowa State University in 2004. The investigators were interested in the effect of using three different fryer oils had on the taste of the fries.

### 3.6 查看reshape2的描述信息

help(package="reshape2")

Package: reshape2

Title: Flexibly Reshape Data: A Reboot of the Reshape Package

Version: 1.4.4

Author: Hadley Wickham <h.wickham@gmail.com>

Maintainer: Hadley Wickham <h.wickham@gmail.com>

Description: Flexibly restructure and aggregate data using just two

functions: melt and 'dcast' (or 'acast').

License: MIT + file LICENSE

URL: https://github.com/hadley/reshape

BugReports: https://github.com/hadley/reshape/issues

Depends: R (>= 3.1)

Imports: plyr (>= 1.8.1), Rcpp, stringr

Suggests: covr, lattice, testthat (>= 0.8.0)

LinkingTo: Rcpp

Encoding: UTF-8

LazyData: true

RoxygenNote: 7.1.0

NeedsCompilation: yes

Packaged: 2020-04-09 12:27:19 UTC; hadley

Repository: CRAN

Date/Publication: 2020-04-09 13:50:02 UTC

Built: R 4.0.0; x86_64-w64-mingw32; 2020-05-02 21:38:15 UTC; windows

Archs: i386, x64

# 4.收尾

sessionInfo()

# R version 4.0.3 (2020-10-10)

# 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] reshape2_1.4.4

#

# loaded via a namespace (and not attached):

#  [1] compiler_4.0.3 magrittr_2.0.1 plyr_1.8.6    tools_4.0.3    yaml_2.2.1

# [6] Rcpp_1.0.6    tinytex_0.29  stringi_1.5.3  stringr_1.4.0  xfun_0.21

长宽数据变换_reshape2包_2021-02-28相关推荐

  1. Stata: 你还在用reshape转换长宽数据吗?那你就OUT了!

    作者:华晨 (The University of Manchester) https://www.lianxh.cn   连享会 - Stata 暑期班 线上直播 9 天:2020.7.28-8.7 ...

  2. ggplot2作图细节(长宽数据转化,数据集挑选,变量排序,调色板限制变量解决方案)

    ggplot2作图细节 我们每个人作图之前都需要将数据整理到位,不然走不到作图哪一步,就进行不下去了.学会前期数据处理比后期图形选择更加重要. 长宽数据概念以及转换 长宽数据在数据运算作图领域非常重要 ...

  3. 推荐两个长宽数据互换函数pivot_longer和pivot_wider

    长宽数据转换有reshape(真难用),reshape2,cast,melt,spread,gather后两项非常简单易用,pivot_longer和pivot_wider,更强劲,在数据重组过程中还 ...

  4. python长数据转换成宽数据_python – 长/宽数据到宽/长

    我有一个数据框,如下所示: import pandas as pd d = {'decil': ['1. decil','1. decil','2. decil','3. decil','3. dec ...

  5. R包reshape2,轻松实现长、宽数据表格转换

    本文翻译自外文博客,原文链接:https://seananderson.ca/2013/10/19/reshape/ 一.reshape2 简介 reshape2是由Hadley Wickham编写的 ...

  6. python长表转换宽表_Pandas数据变换-长宽表互换

    最近工作中,有时会遇到数据转化的问题,这块知识之前掌握得比较零散,今天花时间整理了下 数据来源于下jizhang/pandas-tidy-data​github.com 一.层级索引 层级索引可以作为 ...

  7. tidyr | 长、宽数据的相互转换

    以下图所示的成绩表为例,这种每个样本的信息占据一行的记录方式就是宽数据,它看着非常直观,也是录入数据常使用的形式. 宽数据示意 长数据则是另外一种记录方式,如下图所示,姓名+班级构成了样本标识(表中有 ...

  8. python长表转换宽表_如何用Python来处理数据表的长宽转换(图文详解)

    不多说,直接上干货! 很多地方都需用到这个知识点,比如Tableau里.   通常可以采取如python 和 r来作为数据处理的前期. 数据长宽转换是很常用的需求,特别是当是从Excel中导入的汇总表 ...

  9. 一个基于长数据转化为宽数据的小软件---data_tran.exe

    长宽矩阵在转化的过程中有很多种方式,我最喜欢就是用R或者python去进行转化,当然也可以进行手动转化 首先,我们看一下数据的形式,以及我们想要转化的形式 以A列作为行,B列作为列,E列的值填充在这个 ...

最新文章

  1. mysql query日期_如何获取mysql中两个日期之间的日期列表select query
  2. Java多线程:类ThreadPoolExecutor详解
  3. matplotlib(六)三维作图
  4. 微服务神经元(Neural)
  5. Python3 中的 asyncio async await 概念(实例)(ValueError: too many file descriptors in select())
  6. Android 应用开发(41)---EditText(输入框)详解
  7. windows 7 提示缺少D3DCOMPILER_47.dll的正确解决方法
  8. Picturebox实现图片的缩放
  9. 文章整理 - 匠人精神
  10. 渗透测试工具sqlmap使用技巧-以POST注入为例
  11. 最近大火的「元宇宙」究竟是什么
  12. 简历上如何描述项目经验
  13. 相对湿度和绝对湿度的区别
  14. ISA服务器安装设置全集
  15. demo:猜数字小游戏
  16. 法兰克机器人循环编程_原来可以这样编写Fanuc机器人程序!
  17. 使用Java操纵Excel表格
  18. tshark命令小结
  19. Day1-介绍、基本语法、流程控制
  20. 用事实说话!AJAX应用程序开发七宗罪

热门文章

  1. linux(ubuntu 14.04 lubuntu14.04)下 搜狗拼音法安装后无法运行的解决方法
  2. java中math类的作用_Java开发知识之Java的数字处理类Math类
  3. 织梦(DEDECMS)系统网站搬家技巧-dedeCms换空间搬家教
  4. Razor 一知半解
  5. matlab模拟三体运动_如何写出三体的MATLAB程序-理论分析篇
  6. 几何画板(可绘制复杂函数等)下载方式
  7. 物 理 学 简 介(二)
  8. 【老李的模拟赛】【#6】【2014-08-12】
  9. Java 最全的英文单词单数复数形式转换
  10. php百度收录排名代码,PHP查询百度收录数量代码