長(zhǎng)寬數(shù)據(jù)格式比較_歸納分析

之前寫過(guò)關(guān)于長(zhǎng)寬數(shù)據(jù)轉(zhuǎn)換的文章,而今再看tidyr包,幾乎又迷暈進(jìn)去,所以再次梳理。翻來(lái)覆去的實(shí)踐,其目的在于熟練數(shù)據(jù)之間的自由轉(zhuǎn)換,以便在處理更大數(shù)據(jù)時(shí)不至于迷失。

  • image.png

寬數(shù)據(jù):

  1. 寬數(shù)據(jù)是指數(shù)據(jù)集對(duì)所有的變量進(jìn)行了明確的細(xì)分,各變量的值不存在重復(fù)循環(huán)的情況也無(wú)法歸類。數(shù)據(jù)總體的表現(xiàn)為 變量多而觀察值少。
  2. 每一列為一個(gè)變量,每一行為變量所對(duì)應(yīng)的值。

長(zhǎng)數(shù)據(jù):

  1. 長(zhǎng)數(shù)據(jù)一般是指數(shù)據(jù)集中的變量沒(méi)有做明確的細(xì)分,即變量中至少有一個(gè)變量中的元素存在值嚴(yán)重重復(fù)循環(huán)的情況(可以歸為幾類),表格整體的形狀為長(zhǎng)方形,即 變量少而觀察值多。
  2. 一列包含了所有的變量,而另一列則是與之相關(guān)的值。

1. 長(zhǎng)寬數(shù)據(jù)轉(zhuǎn)換

##########################################################################
#2020-06-24
# 2020-06-25  長(zhǎng)款數(shù)據(jù)格式作圖比較
rm(list = ls())

id <- 1:8
gene <- c(rep("ago", 4), rep("dcr", 4))
value <- c(2.0, 3.0, 2.5, 3.0, 5.0, 4.0, 6.0, 5.5)

dd <- data.frame(id, gene, value)
dd # 長(zhǎng)數(shù)據(jù)
  id gene value
1  1  ago   2.0
2  2  ago   3.0
3  3  ago   2.5
4  4  ago   3.0
5  5  dcr   5.0
6  6  dcr   4.0
7  7  dcr   6.0
8  8  dcr   5.5

dd_2 <- dd %>%
  spread(key = "gene", value = "value")
dd_2 # 寬數(shù)據(jù)
  id ago dcr
1  1 2.0  NA
2  2 3.0  NA
3  3 2.5  NA
4  4 3.0  NA
5  5  NA 5.0
6  6  NA 4.0
7  7  NA 6.0
8  8  NA 5.5

dd_3 <- dd_2 %>%
  gather("ago", "dcr", key = "gene", value = "value")
dd_3 
   id gene value
1   1  ago   2.0
2   2  ago   3.0
3   3  ago   2.5
4   4  ago   3.0
5   5  ago    NA
6   6  ago    NA
7   7  ago    NA
8   8  ago    NA
9   1  dcr    NA
10  2  dcr    NA
11  3  dcr    NA
12  4  dcr    NA
13  5  dcr   5.0
14  6  dcr   4.0
15  7  dcr   6.0
16  8  dcr   5.5

2. 長(zhǎng)寬數(shù)據(jù)繪圖比較

library(ggplot2)
library(ggsignif)
# dd長(zhǎng)數(shù)據(jù)格式,可以直接作圖
compaired <- list(c("ago", "dcr"))
ggplot(dd, aes(x = gene, y = value, fill = gene)) +
  geom_boxplot() +
  geom_signif(comparisons = compaired,
            map_signif_level = T)

# dd_2寬數(shù)據(jù)格式,是不能繪制圖形的

# dd_3長(zhǎng)數(shù)據(jù)格式也是可以的
compaired <- list(c("ago", "dcr"))
ggplot(dd_3, aes(x = gene, y = value, fill = gene)) +
  geom_boxplot() +
  geom_signif(comparisons = compaired,
              map_signif_level = T)
Rplot.png

3. 輸入錄入格式及轉(zhuǎn)換方法

用R繪圖,和以往的GraphPad繪圖時(shí)候,數(shù)據(jù)的錄入方式有些不一樣,所以在一開(kāi)始時(shí)候這個(gè)思維很難轉(zhuǎn)換,甚至沒(méi)有摸清楚數(shù)據(jù)的錄入和繪圖的整體流程。下面圖標(biāo)列出了常見(jiàn)的格式錄入方式。
A: GraphPad類
以GraphPad作圖軟件錄入為例,若按照此類錄入,本質(zhì)上是寬數(shù)據(jù)格式,
需要將其轉(zhuǎn)換為長(zhǎng)數(shù)據(jù)格式,tidyrreshape2兩個(gè)包可用,如下

id <- 1:4
ago <- c(2.0, 3.0, 2.5, 3.0)
dcr <- c( 5.0, 4.0, 6.0, 5.5)
mm <- data.frame(id, ago, dcr)
mm # 寬數(shù)據(jù)

library(tidyverse)
mm2 <- mm%>%
  gather("ago","dcr",key = "gene", value = "value")
mm2

library(reshape2)
mm3 <- mm%>% 
  melt(id.vars = "id", measure.vars = c("ago","dcr"), 
      variable.name = "gene",value.name = "value") 
mm3
 id gene value
1  1  ago   2.0
2  2  ago   3.0
3  3  ago   2.5
4  4  ago   3.0
5  1  dcr   5.0
6  2  dcr   4.0
7  3  dcr   6.0
8  4  dcr   5.5

B: widetype
此處的實(shí)例,在我的之前文章中多處用到。[1][2]

id <- 1:2
gene <- c("ago","dcr")
x1 <- c(2, 5)
x2 <- c(3, 4)
x3 <- c(2.5, 6)
x4 <- c(3, 5.5)
nn <- data.frame(id, gene, x1, x2, x3, x4)
nn

nn2 <- nn %>%
  gather(key = rep,
       value = value, x1:x4, factor_key = TRUE)
nn2

nn3 <- nn %>%
  melt(id.vars = c("id","gene"), measure.vars= c("x1","x2","x3","x4"),
       variable.name = "rep", value.name = "value")
nn3
  id gene rep value
1  1  ago  x1   2.0
2  2  dcr  x1   5.0
3  1  ago  x2   3.0
4  2  dcr  x2   4.0
5  1  ago  x3   2.5
6  2  dcr  x3   6.0
7  1  ago  x4   3.0
8  2  dcr  x4   5.5

C: longtype
長(zhǎng)數(shù)據(jù)格式,沒(méi)有寬數(shù)據(jù)格式可讀性強(qiáng),但是機(jī)器學(xué)習(xí)需要的就是長(zhǎng)數(shù)據(jù)格式,以SPSS等為代表。

image.png

  1. R繪圖應(yīng)用實(shí)例:數(shù)據(jù)錄入、轉(zhuǎn)換及繪圖 ?

  2. 數(shù)據(jù)錄入與格式轉(zhuǎn)換(reshape2、tidyr包) ?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容