我有data.frame
df
,包含4个网站。每个站点有四个参数(A,B,C和D),我有2011年到2014年每个参数的值
siteID <- c(rep("site1", 16), rep("site2", 16), rep("site3", 16),rep("site4", 16))
YEAR <- rep(c("2011", "2012", "2013", "2014"), 16)
parameter <- c(rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4),
rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4),
rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4),
rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4))
value <- c(seq(1, 4, by=1), seq(10, 40, by=10), seq(12, 18, by=2), seq(5, 20, by=5),
seq(3, 12, by=3), sample(13:18, 4), sample(15:22, 4), sample(10:18, 4),
seq(7, 1, by=-2), sample(15:22, 4), sample(15:19, 4), sample(10:20, 4),
seq(8, 5, by=-1), seq(50, 20, by=-10), seq(16, 10, by=-2), seq(20, 5, by=-5))
df <- data.frame(siteID, YEAR, parameter, value)
head(df, 20)
# siteID YEAR parameter value
#1 site1 2011 A 1
#2 site1 2012 A 2
#3 site1 2013 A 3
#4 site1 2014 A 4
#5 site1 2011 B 10
#6 site1 2012 B 20
#7 site1 2013 B 30
#8 site1 2014 B 40
#9 site1 2011 C 12
#10 site1 2012 C 14
#11 site1 2013 C 16
#12 site1 2014 C 18
#13 site1 2011 D 5
#14 site1 2012 D 10
#15 site1 2013 D 15
#16 site1 2014 D 20
#17 site2 2011 A 3
#18 site2 2012 A 6
#19 site2 2013 A 9
#20 site2 2014 A 12
我可以通过下面的图表了解数据的趋势
library(ggplot2)
ggplot(df, aes(x = YEAR, y= value, col= parameter))+
geom_line(aes(group=parameter))+
facet_wrap(~siteID)
但是,如果我有很多站点(例如70个站点),那么将花费更多时间来直观地分析所有参数的趋势。无论如何,我可以在下表中的数据中获得数据趋势
siteID parameter trend
site1 A increasing
site1 B decreasing
site1 C no trend
site1 D increasing
.
site4 A decreasing
site4 B decreasing
site4 C increasing
site4 D decreasing
任何建议都会受到赞赏吗?
答案 0 :(得分:1)
基本解决方案是将数据框转换为宽格式,然后添加一列,在该列中建立增加,减少或不增加趋势的条件。在示例中,如果变化为+ -2,则没有趋势。
library(reshape2)
df2 <- dcast(df, siteID + parameter ~ YEAR, value.var="value")
df2$trend <- ifelse(df2$`2014`-df2$`2011`>2, "increasing",
ifelse(df2$`2014`-df2$`2011`< -2, "decreasing", "no trend"))
df2
输出:
siteID parameter 2011 2012 2013 2014 trend
1 site1 A 1 2 3 4 increasing
2 site1 B 10 20 30 40 increasing
3 site1 C 12 14 16 18 increasing
4 site1 D 5 10 15 20 increasing
5 site2 A 3 6 9 12 increasing
6 site2 B 16 17 14 15 no trend
7 site2 C 16 20 18 19 increasing
8 site2 D 16 13 11 12 decreasing
9 site3 A 7 5 3 1 decreasing
10 site3 B 20 22 17 16 decreasing
11 site3 C 17 19 15 18 no trend
12 site3 D 19 12 17 20 no trend
13 site4 A 8 7 6 5 decreasing
14 site4 B 50 40 30 20 decreasing
15 site4 C 16 14 12 10 decreasing
16 site4 D 20 15 10 5 decreasing