之前寫(xiě)過(guò)關(guān)于長(zhǎng)寬數(shù)據(jù)轉(zhuǎn)換的文章,而今再看tidyr包挂滓,幾乎又迷暈進(jìn)去,所以再次梳理啸胧。翻來(lái)覆去的實(shí)踐赶站,其目的在于熟練數(shù)據(jù)之間的自由轉(zhuǎn)換,以便在處理更大數(shù)據(jù)時(shí)不至于迷失纺念。
寬數(shù)據(jù):
- 寬數(shù)據(jù)是指數(shù)據(jù)集對(duì)所有的變量進(jìn)行了明確的細(xì)分贝椿,各變量的值不存在重復(fù)循環(huán)的情況也無(wú)法歸類(lèi)。數(shù)據(jù)總體的表現(xiàn)為 變量多而觀(guān)察值少陷谱。
- 每一列為一個(gè)變量烙博,每一行為變量所對(duì)應(yīng)的值。
長(zhǎng)數(shù)據(jù):
- 長(zhǎng)數(shù)據(jù)一般是指數(shù)據(jù)集中的變量沒(méi)有做明確的細(xì)分烟逊,即變量中至少有一個(gè)變量中的元素存在值嚴(yán)重重復(fù)循環(huán)的情況(可以歸為幾類(lèi))渣窜,表格整體的形狀為長(zhǎng)方形,即 變量少而觀(guān)察值多宪躯。
- 一列包含了所有的變量乔宿,而另一列則是與之相關(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)
3. 輸入錄入格式及轉(zhuǎn)換方法
用R繪圖,和以往的GraphPad繪圖時(shí)候臣缀,數(shù)據(jù)的錄入方式有些不一樣蛤虐,所以在一開(kāi)始時(shí)候這個(gè)思維很難轉(zhuǎn)換,甚至沒(méi)有摸清楚數(shù)據(jù)的錄入和繪圖的整體流程肝陪。下面圖標(biāo)列出了常見(jiàn)的格式錄入方式。
A: GraphPad類(lèi)
以GraphPad作圖軟件錄入為例刑顺,若按照此類(lèi)錄入氯窍,本質(zhì)上是寬數(shù)據(jù)格式,
需要將其轉(zhuǎn)換為長(zhǎng)數(shù)據(jù)格式蹲堂,tidyr和reshape2兩個(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等為代表布隔。