0

我有一点 data.frame (现在是 xts 的时间戳,我希望这不是问题),像这样:

> head(testData)
                    Year Day Hour  Min   Sec.    E1.S1    E1.S2 E1.S3  E1.S4     E1.S5  E1.S6
2000-05-01 10:25:59 2000 122   10 25.0 35.144 3.572000 2.912000 2.512  6.352 10.604000  8.078
2000-05-01 10:26:48 2000 122   10 26.0 31.570 2.203333 2.423333 6.160 13.000  9.463333  3.970
2000-05-01 10:27:48 2000 122   10 26.8 35.964 2.118000 3.044000 4.232 11.770 12.696000  6.088
                      E1.S7    E1.S8  E2.S1  E2.S2   E2.S3     E2.S4     E2.S5  E2.S6     E2.S7
2000-05-01 10:25:59  3.0444 3.574000 0.9244 1.5868  1.3224  1.852800  2.646000  2.910  2.382000
2000-05-01 10:26:48  3.3100 3.083333 1.1000 1.5440  1.9800  2.646667  2.646667  3.310  1.763333
2000-05-01 10:27:48  2.6440 2.646000 1.8528 1.8524  2.9120  2.648000  3.970000  5.292  1.850000
                        E2.S8      E3.S1     E3.S2   E3.S3     E3.S4     E3.S5   E3.S6   E3.S7
2000-05-01 10:25:59  1.586000  0.7920000 1.0568000  1.7184  1.720400  2.116400  0.9248  2.1180
2000-05-01 10:26:48  1.983333  0.4413333 0.2206667  1.5400  1.100667  2.646667  0.8800  0.8800
2000-05-01 10:27:48  1.452000  0.9264000 1.1908000  2.5140  1.718400  2.250400  2.2504  1.8504
                      E3.S8      E4.S1      E4.S2   E4.S3   E4.S4      E4.S5     E4.S6      E4.S7
2000-05-01 10:25:59  1.1880  0.9252000  0.1324000  0.6612  0.6604  0.5288000  0.132400  0.6612000
2000-05-01 10:26:48  2.2040  0.2206667  0.2206667  0.4400  0.4400  0.8813333  1.100667  0.8813333
2000-05-01 10:27:48  0.9240  0.7936000  0.7928000  0.9244  1.3208  0.6612000  0.132400  0.5288000
                      E4.S8    FP5.S1 FP5.S2    FP5.S3    FP5.S4    FP5.S5     FP5.S6  FP5.S7
2000-05-01 10:25:59  1.0560 0.0662000 0.3310 0.3968000 0.0662000 0.0000000  0.2648000 0.19860
2000-05-01 10:26:48  1.1000 0.1103333 0.3310 0.2206667 0.1103333 0.4413333  0.2206667 0.33100
2000-05-01 10:27:48  0.5288 0.2648000 0.4632 0.0000000 0.4632000 0.2648000  0.2648000 0.72660
                       FP5.S8 FP6.S1    FP6.S2 FP6.S3  FP6.S4    FP6.S5 FP6.S6 FP6.S7    FP6.S8
2000-05-01 10:25:59 0.2648000 0.1324 0.1986000 0.2648 0.00000 0.1324000 0.1324 0.1324 0.0662000
2000-05-01 10:26:48 0.2206667 0.0000 0.1103333 0.0000 0.00000 0.2206667 0.0000 0.0000 0.1103333
2000-05-01 10:27:48 0.1324000 0.0000 0.0000000 0.0000 0.00000 0.1986000 0.1324 0.1324 0.0662000
                    FP7.S1 FP7.S2  FP7.S3 FP7.S4    FP7.S5    FP7.S6 FP7.S7  FP7.S8 PA.LEFS60S1
2000-05-01 10:25:59 0.1324 0.0000 0.00000 0.0000 0.1324000 0.3310000 0.0000 0.39680       79.46
2000-05-01 10:26:48 0.0000 0.0000 0.00000 0.0000 0.4413333 0.1103333 0.0000 0.00000       78.50
2000-05-01 10:27:48 0.0000 0.3972 0.00000 0.1324 0.1324000 0.1986000 0.1324 0.00000       84.10
                    PA.LEFS60S2 PA.LEFS60S3 PA.LEFS60S4 PA.LEFS60S5 PA.LEFS60S6 PA.LEFS60S7
2000-05-01 10:25:59    83.26000      103.48    131.6000    157.6000    148.6000   120.60000
2000-05-01 10:26:48    99.93333      130.00    160.6667    153.6667    121.6667    92.96667
2000-05-01 10:27:48    95.68000      118.60    144.6000    155.0000    134.6000   109.10000
                    PA.LEFS60S8       BX         BY       BZ Bmag....nT.
2000-05-01 10:25:59    94.44000 3.608000  2.6620000 5.032000      6.8840
2000-05-01 10:26:48    75.63333 4.943333 -0.5133333 4.816667      6.9300
2000-05-01 10:27:48    89.44000 3.908000  0.9634000 5.490000      6.8460

> dput(head(testData))
structure(c(2000, 2000, 2000, 2000, 2000, 2000, 122, 122, 122, 
122, 122, 122, 10, 10, 10, 10, 10, 10, 25, 26, 26.8, 28, 29, 
30, 35.144, 31.57, 35.964, 21.4, 24.892, 25.354, 3.572, 2.20333333333333, 
2.118, 3.8025, 4.628, 5.292, 2.912, 2.42333333333333, 3.044, 
1.488, 3.97, 5.428, 2.512, 6.16, 4.232, 4.465, 3.836, 7.54, 6.352, 
13, 11.77, 7.775, 8.604, 14.16, 10.604, 9.46333333333333, 12.696, 
10.93, 16.68, 23.96, 8.078, 3.97, 6.088, 10.425, 15.612, 29.1, 
3.0444, 3.31, 2.644, 5.135, 10.32, 20.612, 3.574, 3.08333333333333, 
2.646, 3.1425, 3.966, 9.918, 0.9244, 1.1, 1.8528, 2.645, 4.368, 
8.476, 1.5868, 1.544, 1.8524, 2.315, 3.572, 6.218, 1.3224, 1.98, 
2.912, 3.4725, 6.34, 12.832, 1.8528, 2.64666666666667, 2.648, 
6.4475, 8.86, 24.74, 2.646, 2.64666666666667, 3.97, 7.275, 12.948, 
30.7, 2.91, 3.31, 5.292, 7.44, 12.988, 30.42, 2.382, 1.76333333333333, 
1.85, 3.315, 12.174, 32.28, 1.586, 1.98333333333333, 1.452, 2.81, 
7.286, 17.72, 0.792, 0.441333333333333, 0.9264, 1.655, 4.234, 
10.194, 1.0568, 0.220666666666667, 1.1908, 1.1555, 3.8388, 7.016, 
1.7184, 1.54, 2.514, 3.3105, 8.192, 14.004, 1.7204, 1.10066666666667, 
1.7184, 4.1375, 11.504, 37.44, 2.1164, 2.64666666666667, 2.2504, 
4.135, 17.876, 51.74, 0.9248, 0.88, 2.2504, 4.4675, 18.28, 52.38, 
2.118, 0.88, 1.8504, 5.465, 14.696, 52.12, 1.188, 2.204, 0.924, 
3.1425, 10.73, 30.82, 0.9252, 0.220666666666667, 0.7936, 0.331, 
5.03, 14.152, 0.1324, 0.220666666666667, 0.7928, 1.983, 3.97, 
10.714, 0.6612, 0.44, 0.9244, 1.653, 10.196, 20.1, 0.6604, 0.44, 
1.3208, 3.9675, 20.76, 54.78, 0.5288, 0.881333333333333, 0.6612, 
4.47, 20.52, 68, 0.1324, 1.10066666666667, 0.1324, 5.7925, 21.44, 
78.42, 0.6612, 0.881333333333333, 0.5288, 3.4775, 24.604, 76.12, 
1.056, 1.1, 0.5288, 2.313, 15.082, 42.86, 0.0662, 0.110333333333333, 
0.2648, 0.579, 1.6552, 3.838, 0.331, 0.331, 0.4632, 1.1565, 2.446, 
2.054, 0.3968, 0.220666666666667, 0, 0.41375, 1.7862, 3.97, 0.0662, 
0.110333333333333, 0.4632, 0.9935, 2.976, 9.792, 0, 0.441333333333333, 
0.2648, 0.6605, 2.78, 9.672, 0.2648, 0.220666666666667, 0.2648, 
0.74425, 2.9782, 10.592, 0.1986, 0.331, 0.7266, 1.32375, 4.036, 
9.136, 0.2648, 0.220666666666667, 0.1324, 0.908, 2.7144, 5.292, 
0.1324, 0, 0, 0, 0, 0.1986, 0.1986, 0.110333333333333, 0, 0.1655, 
0, 0, 0.2648, 0, 0, 0.1655, 0, 0, 0, 0, 0, 0.08275, 0.331, 0.1324, 
0.1324, 0.220666666666667, 0.1986, 0.24825, 0.2648, 0.0662, 0.1324, 
0, 0.1324, 0.331, 0.3972, 0.662, 0.1324, 0, 0.1324, 0, 0.1324, 
0.1324, 0.0662, 0.110333333333333, 0.0662, 0.1655, 0.0662, 0.1324, 
0.1324, 0, 0, 0, 0, 0.1324, 0, 0, 0.3972, 0.1655, 0.3968, 0, 
0, 0, 0, 0.08275, 0.0662, 0.0662, 0, 0, 0.1324, 0.1655, 0, 0.2648, 
0.1324, 0.441333333333333, 0.1324, 0, 0.1324, 0.0662, 0.331, 
0.110333333333333, 0.1986, 0.331, 0.1986, 0.1986, 0, 0, 0.1324, 
0, 0.2648, 0.1986, 0.3968, 0, 0, 0.08275, 0.1986, 0, 79.46, 78.5, 
84.1, 89.2, 90.06, 93.8, 83.26, 99.9333333333333, 95.68, 92.4, 
90.08, 87.88, 103.48, 130, 118.6, 108.75, 104.42, 97.36, 131.6, 
160.666666666667, 144.6, 131.75, 127, 118, 157.6, 153.666666666667, 
155, 149.75, 147.6, 141.2, 148.6, 121.666666666667, 134.6, 144, 
147.6, 151.8, 120.6, 92.9666666666667, 109.1, 122, 127, 136, 
94.44, 75.6333333333333, 89.44, 100.775, 104.4, 112.2, 3.608, 
4.94333333333333, 3.908, 2.885, 2.548, 1.708, 2.662, -0.513333333333333, 
0.9634, 2.0675, 2.544, 3.208, 5.032, 4.81666666666667, 5.49, 
5.7875, 5.776, 5.768, 6.884, 6.93, 6.846, 6.8025, 6.838, 6.824
), .indexCLASS = c("POSIXlt", "POSIXt"), .indexTZ = "", tclass = c("POSIXlt", 
"POSIXt"), tzone = "", class = c("xts", "zoo"), index = structure(c(957173159, 
957173208, 957173268, 957173329, 957173389, 957173450), tzone = "", tclass = c("POSIXlt", 
"POSIXt")), .Dim = c(6L, 73L), .Dimnames = list(NULL, c("Year", 
"Day", "Hour", "Min", "Sec.", "E1.S1", "E1.S2", "E1.S3", "E1.S4", 
"E1.S5", "E1.S6", "E1.S7", "E1.S8", "E2.S1", "E2.S2", "E2.S3", 
"E2.S4", "E2.S5", "E2.S6", "E2.S7", "E2.S8", "E3.S1", "E3.S2", 
"E3.S3", "E3.S4", "E3.S5", "E3.S6", "E3.S7", "E3.S8", "E4.S1", 
"E4.S2", "E4.S3", "E4.S4", "E4.S5", "E4.S6", "E4.S7", "E4.S8", 
"FP5.S1", "FP5.S2", "FP5.S3", "FP5.S4", "FP5.S5", "FP5.S6", "FP5.S7", 
"FP5.S8", "FP6.S1", "FP6.S2", "FP6.S3", "FP6.S4", "FP6.S5", "FP6.S6", 
"FP6.S7", "FP6.S8", "FP7.S1", "FP7.S2", "FP7.S3", "FP7.S4", "FP7.S5", 
"FP7.S6", "FP7.S7", "FP7.S8", "PA.LEFS60S1", "PA.LEFS60S2", "PA.LEFS60S3", 
"PA.LEFS60S4", "PA.LEFS60S5", "PA.LEFS60S6", "PA.LEFS60S7", "PA.LEFS60S8", 
"BX", "BY", "BZ", "Bmag....nT.")))

对于每个时间戳,我需要绘制(并获取一些统计信息)E1.S* 值与 PA.LEFS60S* 值的 cos。例如,对于时间戳 2000-05-01 10:25:59:

# These are the values from PA.LEFS60S1 to PA.LEFS60S8    
x = c(79.46, 83.26000, 103.48, 131.6000, 157.6000, 148.6000, 120.60000, 94.44000)
# These are the values from E1.S1 to E1.S8
y = c(3.572000, 2.912000, 2.512, 6.352, 10.604000, 8.078,3.0444, 3.574000)
plot(cos(x),y)

然后我需要在一段时间内绘制平均值(y)与时间戳之类的东西......我只能在我获得一种自动化过程的方法之后才能做到这一点。

到目前为止,我只能手动完成。尽管我可以想出一种方法来使用 for 循环并检查表中的每个名称。但这似乎在计算上太慢太复杂,根本不像 R。

我尝试了使用rshape2包中的melt功能 -但输出只是来自多行以 timeStamp 作为索引且没有列信息的行。melt(testData)

4

1 回答 1

1

这是您的问题的解决方案。它涉及一些数据重塑。

首先,将存储在值中的数据转换为行。这是单独为xy数据完成的。

x <- stack(as.data.frame(t(testData[ , grep("^PA", names(testData))])))
y <- stack(as.data.frame(t(testData[ , grep("^E1", names(testData))])))

将数据合并到一个数据框中:

xydf <- setNames(cbind(x, y[-2]), c("x", "time", "y"))

图1

绘制不同时间戳的y对比图。cos(x)使用该ggplot2软件包很容易。

library(ggplot2)
ggplot(xydf, aes(x = cos(x), y = y)) +
  geom_point() +
  facet_wrap( ~ time)

在此处输入图像描述

图 2

y计算并绘制沿时间戳的平均值。

yMeans <- aggregate(y ~ time, xydf, mean)

ggplot(yMeans, aes(x = time(testData), y = y)) + 
  geom_point() +
  geom_line()

在此处输入图像描述

于 2013-03-19T14:21:57.277 回答