重塑数据框 - 更改列和行
Reshape Dataframe - Change columns and rows
我有一个如下所示的数据框:
现在我想将 Stations 作为列,一列用于 Station 1,一列用于 Station 2,一列用于 Station 3。
日期应该在行中,就像这样:
Date Day Station 1 Station 2 Station 3
2000-01-01 Day1 5 15 20
2000-01-01 Day2 4.0 8.8 11.2
2000-01-01 Day3 10 30 40
2000-02-01 Day1 10 NA 25
2000-02-01 Day2 6.4 NA 13.6
2000-02-01 Day3 20 NA 50
2000-03-01 Day1 NA NA 30
2000-03-01 Day2 NA NA 16.0
2000-03-01 Day3 NA NA 60
我以前使用过 reshape(df),但我不知道如何调整它以使其在这种情况下起作用。
有什么想法吗?
这是我的测试框架的代码:
x1= c("Station 1", "Station 1", "Station 2", "Station 3", "Station 3", "Station 3")
x2= c("2000-01-01", "2000-02-01", "2000-01-01", "2000-01-01", "2000-02-01", "2000-03-01")
x3= seq(5, 30, length=6)
x4= seq(4, 16, length=6)
x5= seq(10, 60, length=6)
testframe = data.frame(Station=x1, Date=x2, Day1=x3, Day2=x4, Day3=x5)
向 Nate 致歉,但这是解决此问题的非常糟糕的方法。正确的 "reshape2" 方法应该是以下之一:
library(reshape2)
dcast(melt(testframe, id.vars = 1:2), Date + variable ~ Station)
recast(testframe, Date + variable ~ Station, id.var = c("Station", "Date"))
两者的结果如下:
# Date variable Station 1 Station 2 Station 3
# 1 2000-01-01 Day1 5.0 15.0 20.0
# 2 2000-01-01 Day2 4.0 8.8 11.2
# 3 2000-01-01 Day3 10.0 30.0 40.0
# 4 2000-02-01 Day1 10.0 NA 25.0
# 5 2000-02-01 Day2 6.4 NA 13.6
# 6 2000-02-01 Day3 20.0 NA 50.0
# 7 2000-03-01 Day1 NA NA 30.0
# 8 2000-03-01 Day2 NA NA 16.0
# 9 2000-03-01 Day3 NA NA 60.0
或者,按照 eipi10 的建议,您也可以使用 "tidyverse" 包:
library(tidyverse)
testframe %>%
gather(Day, value, Day1:Day3) %>%
spread(Station, value)
我有一个如下所示的数据框:
现在我想将 Stations 作为列,一列用于 Station 1,一列用于 Station 2,一列用于 Station 3。
日期应该在行中,就像这样:
Date Day Station 1 Station 2 Station 3
2000-01-01 Day1 5 15 20
2000-01-01 Day2 4.0 8.8 11.2
2000-01-01 Day3 10 30 40
2000-02-01 Day1 10 NA 25
2000-02-01 Day2 6.4 NA 13.6
2000-02-01 Day3 20 NA 50
2000-03-01 Day1 NA NA 30
2000-03-01 Day2 NA NA 16.0
2000-03-01 Day3 NA NA 60
我以前使用过 reshape(df),但我不知道如何调整它以使其在这种情况下起作用。
有什么想法吗?
这是我的测试框架的代码:
x1= c("Station 1", "Station 1", "Station 2", "Station 3", "Station 3", "Station 3")
x2= c("2000-01-01", "2000-02-01", "2000-01-01", "2000-01-01", "2000-02-01", "2000-03-01")
x3= seq(5, 30, length=6)
x4= seq(4, 16, length=6)
x5= seq(10, 60, length=6)
testframe = data.frame(Station=x1, Date=x2, Day1=x3, Day2=x4, Day3=x5)
向 Nate 致歉,但这是解决此问题的非常糟糕的方法。正确的 "reshape2" 方法应该是以下之一:
library(reshape2)
dcast(melt(testframe, id.vars = 1:2), Date + variable ~ Station)
recast(testframe, Date + variable ~ Station, id.var = c("Station", "Date"))
两者的结果如下:
# Date variable Station 1 Station 2 Station 3
# 1 2000-01-01 Day1 5.0 15.0 20.0
# 2 2000-01-01 Day2 4.0 8.8 11.2
# 3 2000-01-01 Day3 10.0 30.0 40.0
# 4 2000-02-01 Day1 10.0 NA 25.0
# 5 2000-02-01 Day2 6.4 NA 13.6
# 6 2000-02-01 Day3 20.0 NA 50.0
# 7 2000-03-01 Day1 NA NA 30.0
# 8 2000-03-01 Day2 NA NA 16.0
# 9 2000-03-01 Day3 NA NA 60.0
或者,按照 eipi10 的建议,您也可以使用 "tidyverse" 包:
library(tidyverse)
testframe %>%
gather(Day, value, Day1:Day3) %>%
spread(Station, value)