统计分析方法

数据排序与长宽型数据的转换

2021-06-09  本文已影响0人  Hayley笔记

1. 数据排序

1. sort()函数

对数值进行排序

x <- sample(1:100,10)
x
#  [1] 19 78 65 40 52 89 62 71 32 76
sort(x)
#  [1] 19 32 40 52 62 65 71 76 78 89
sort(x,decreasing = T)
#  [1] 89 78 76 71 65 62 52 40 32 19

对字符串进行排序(按首字母顺序)

y <- c('python','ruby','java','r')
y
# [1] "python" "ruby"   "java"   "r" 
sort(y)
# [1] "java"   "python" "r"      "ruby"  
1.2 rank()函数(秩次)

❗️取秩次在秩和检验中用得到

x
#  [1] 19 78 65 40 52 89 62 71 32 76
rank(x)
#  [1]  1  9  6  3  4 10  5  7  2  8
# 返回的是秩次,也就是按从小到大排列后,19是排在1位,78排在9位,依次类推。
z <- c(1,2,3,3,4,4,5,6,6,6,7,8,8)
rank(z)
#  [1]  1.0  2.0  3.5  3.5  5.5  5.5  7.0  9.0  9.0  9.0 11.0
# [12] 12.5 12.5
# 出现相同的值时,秩次也取均值,相同的值秩次相同。
1.3 order()函数(常用⚠️)
x
#  [1] 19 78 65 40 52 89 62 71 32 76
order(x)
#  [1]  1  9  4  5  7  3  8 10  2  6
# 返回的是原始x的下标。也就是从小到大排列后,排第一的是x的第1个元素,排第二的是x的第9个元素,依此类推。
x[order(x)] #order(x)先返回排序后的下标,再用这个下标对x进行排序,相当于sort。
#  [1] 19 32 40 52 62 65 71 76 78 89

应用于数据框⚠️(sort只能对向量进行排序,order返回的是下标,因此可以对数据框排序)

# 根据Sepal.Length对数据框进行排序
head(iris)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa

head(iris[order(iris$Sepal.Length),])
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 14          4.3         3.0          1.1         0.1  setosa
# 9           4.4         2.9          1.4         0.2  setosa
# 39          4.4         3.0          1.3         0.2  setosa
# 43          4.4         3.2          1.3         0.2  setosa
# 42          4.5         2.3          1.3         0.3  setosa
# 4           4.6         3.1          1.5         0.2  setosa

2. 长宽型数据的转换

2.1 stack()函数
freshmen <- c(178,180,182,180)
sophomores <- c(188,172,175,172)
juniors <- c(167,172,177,174)
df <- data.frame(fresh=freshmen,sopho=sophomores,jun=juniors)
df #宽型数据
#   fresh sopho jun
# 1   178   188 167
# 2   180   172 172
# 3   182   175 177
# 4   180   172 174

height <- stack(list(fresh=freshmen,sopho=sophomores,jun=juniors))
height #长型数据
#    values   ind
# 1     178 fresh
# 2     180 fresh
# 3     182 fresh
# 4     180 fresh
# 5     188 sopho
# 6     172 sopho
# 7     175 sopho
# 8     172 sopho
# 9     167   jun
# 10    172   jun
# 11    177   jun
# 12    174   jun

长型数据方便汇总计算

tapply(height$values,height$ind,mean)
#  fresh  sopho    jun 
# 180.00 176.75 172.50 
2.2 reshape()函数

将长型数据Indometh转换成宽型数据

wide <- reshape(Indometh,v.names = 'conc',idvar = 'Subject',
                timevar = 'time',direction = 'wide')
# v.names参数是把哪个变量设置成value(变成数据框后填在数据框中的值)
# id.var参数设置标示变量(分行)
# timevar参数设置v.names是根据谁来重复的(分列)
# direction设置转换成长型还是宽型

#长变宽
head(wide)
#    Subject conc.0.25 conc.0.5 conc.0.75 conc.1 conc.1.25 conc.2 conc.3 conc.4 conc.5 conc.6 conc.8
# 1        1      1.50     0.94      0.78   0.48      0.37   0.19   0.12   0.11   0.08   0.07   0.05
# 12       2      2.03     1.63      0.71   0.70      0.64   0.36   0.32   0.20   0.25   0.12   0.08
# 23       3      2.72     1.49      1.16   0.80      0.80   0.39   0.22   0.12   0.11   0.08   0.08
# 34       4      1.85     1.39      1.02   0.89      0.59   0.40   0.16   0.11   0.10   0.07   0.07
# 45       5      2.05     1.04      0.81   0.39      0.30   0.23   0.13   0.11   0.08   0.10   0.06
# 56       6      2.31     1.44      1.03   0.84      0.64   0.42   0.24   0.17   0.13   0.10   0.09

#宽变长
long=reshape(wide, idvar = "Subject", varying = list(2:12),v.names = "conc", direction = "long")
# 和原始Indometh不太一样
2.3 reshape2包的melt()函数和decast()函数

⚠️tidyr也可以进行长宽型数值的转换且功能更强大,可以用来替代reshape2

head(iris)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa

new_iris <- melt(data = iris,id.vars = 'Species') 
head(new_iris)
#   Species     variable value
# 1  setosa Sepal.Length   5.1
# 2  setosa Sepal.Length   4.9
# 3  setosa Sepal.Length   4.7
# 4  setosa Sepal.Length   4.6
# 5  setosa Sepal.Length   5.0
# 6  setosa Sepal.Length   5.4
dcast(new_iris,formula = Species~variable,fun.aggregate = mean,value.var = 'value')
#      Species Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1     setosa        5.006       3.428        1.462       0.246
# 2 versicolor        5.936       2.770        4.260       1.326
# 3  virginica        6.588       2.974        5.552       2.026
上一篇下一篇

猜你喜欢

热点阅读