[英]Rearranging data frame for a Heat map plot in R
I am trying to plot a heat map with ggpolt and to do this I want to rearrange my data frame that like this:我正在尝试使用 ggpolt 绘制热图,为此我想重新排列我的数据框,如下所示:
country 2012 2013 2014 2015
AUS 2 5 6 1
AUT 3 3 1 5
BEL 1 8 2 8
NED 5 3 0 5
into a date frame that looks like this:进入一个看起来像这样的日期框架:
country year value
AUS 2012 2
AUS 2013 5
AUS 2014 6
AUS 2015 1
AUT 2012 3
AUT 2013 3
AUT 2014 1
AUT 2015 5
BEL 2012 1
BEL 2013 8
BEL 2014 2
BEL 2015 8
NED 2012 5
NED 2013 3
NED 2014 0
NED 2014 5
Namely, from a data frame with rows of years to a three-column data frame of country, year and a corresponding value.即,从具有年份行的数据框到国家、年份和相应值的三列数据框。
THANKS谢谢
We can use pivot_longer
我们可以使用pivot_longer
library(tidyr)
pivot_longer(df1, cols = -country, names_to = 'year')
# country year value
#1 AUS 2012 2
#2 AUS 2013 5
#3 AUS 2014 6
#4 AUS 2015 1
#5 AUT 2012 3
#6 AUT 2013 3
#7 AUT 2014 1
#8 AUT 2015 5
#9 BEL 2012 1
#10 BEL 2013 8
#11 BEL 2014 2
#12 BEL 2015 8
#13 NED 2012 5
#14 NED 2013 3
#15 NED 2014 0
#16 NED 2015 5
df1 <- structure(list(country = c("AUS", "AUT", "BEL", "NED"), `2012` = c(2L,
3L, 1L, 5L), `2013` = c(5L, 3L, 8L, 3L), `2014` = c(6L, 1L, 2L,
0L), `2015` = c(1L, 5L, 8L, 5L)), class = "data.frame", row.names = c(NA,
-4L))
Using melt
:使用melt
:
library(data.table)
setDT(df); melt(df, id.vars = "country", variable.name = "year")
country year value
#1: AUS 2012 2
#2: AUT 2012 3
#3: BEL 2012 1
#4: NED 2012 5
#5: AUS 2013 5
#6: AUT 2013 3
#7: BEL 2013 8
#8: NED 2013 3
#9: AUS 2014 6
#10: AUT 2014 1
#11: BEL 2014 2
#12: NED 2014 0
#13: AUS 2015 1
#14: AUT 2015 5
#15: BEL 2015 8
#16: NED 2015 5
df <- structure(list(country = structure(1:4, .Label = c("AUS", "AUT", "BEL", "NED"), class = "factor"), `2012` = c(2L, 3L, 1L, 5L), `2013` = c(5L, 3L, 8L, 3L), `2014` = c(6L, 1L, 2L, 0L), `2015` = c(1L,5L, 8L, 5L)), class = "data.frame", row.names = c(NA, -4L))
A base R solution is this (re-using @akrun's data):一个基本的 R 解决方案是这样的(重新使用@akrun 的数据):
First, unlist
values in df1[,2:5]
and store in vector:首先, unlist
df1[,2:5]
中的值并存储在向量中:
values <- as.numeric(unlist(df1[,2:5]))
Next, repeat country values and year values an appropriate number of times and store in vectors:接下来,将国家/地区值和年份值重复适当次数并存储在向量中:
countries <- rep(df1$country, length(counts)/length(df1$country))
years <- rep(names(df1[,2:5]),length(counts)/length(df1$country))
Then combine all three vectors in new dataframe:然后将所有三个向量组合到新的数据框中:
df1_long <-data.frame(countries, years, values)
Finally, order df1_long
in (default) aphabetical order of df1_long$countries
:最后,以df1_long
df1_long$countries
的(默认)非字母顺序排列 df1_long:
df1_long_ord <- df1_long[order(df1_long$countries),]
Result:结果:
df1_long_ord
countries years values
1 AUS 2012 2
5 AUS 2012 5
9 AUS 2012 6
13 AUS 2012 1
2 AUT 2013 3
6 AUT 2013 3
10 AUT 2013 1
14 AUT 2013 5
3 BEL 2014 1
7 BEL 2014 8
11 BEL 2014 2
15 BEL 2014 8
4 NED 2015 5
8 NED 2015 3
12 NED 2015 0
16 NED 2015 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.