Selecting just some rows of a RGList - r

I am using the package Limma to analyse some data. After reading the raw data with read.maimagenes I get a RGList object. R.cut and G.cut are a value of class numeric and I want the values above them. I tried something like this:
RG$R <- RG$R[RG$R>R.cut]
RG$G <- RG$G[RG$G>G.cut]
But this converts the class of RG$R from matrix to numeric how could I keep the matrix class (I don't know if introducing some NA values would affect the rest of the analyse). I have tried subset like this:
RG.probe$R <- subset(x=RG, subset=RG$R>R.cut)
But it keeps returning an error Error: Two subscripts required
How can this be done?
Aside: If I would like to just get the rows that are above the R.cut and G.cut?
Possible solution:
I have found that with ifelse I can do it although I must introduce a value for the ones that are under the cut, and I haven't found the way to check if both R and G channels are above each cut.
RG$R<-ifelse(RG$R>R.cut, RG$R, '')
RG$G<-ifelse(RG$G>G.cut, RG$G, '')
Although it is converted to character, and therefore I cannot do further analysis.
Data to make it reproducible:
library(limma)
RG<-read.maimages(path, source='agilent')
class(RG)
[1] "RGList"
attr(,"package")
[1] "limma"
dput(head(RG$R))
structure(c(2893, 81.5, 80.5, 140208, 4512, 6272, 4934, 195,
184.5, 164092, 11819, 10569, 1689.5, 83, 82, 68996, 2260.5, 3603,
2470, 84, 77, 96750, 3203, 5223, 3246, 85.5, 104.5, 54773, 519.5,
8244.5, 1807, 86.5, 88, 204574, 15693, 8939.5, 2040, 87, 95,
131880, 7346, 9922.5, 1445, 76, 85.5, 125598, 3863, 5758.5, 2626,
87.5, 85, 180266, 18173, 20171.5, 1811.5, 84, 87.5, 122498, 3993,
5857, 1799, 87.5, 82, 123220, 3780, 5964, 1706, 77.5, 80, 124463,
3390, 5070, 3787, 81.5, 88, 65874, 269, 781.5, 1476, 90, 89,
122445, 4232, 6479, 2788, 82, 87.5, 80669, 791, 7440.5, 1503,
81, 88, 124702, 4270, 6111, 2012.5, 93.5, 90, 215820, 4555, 3101,
1727.5, 102, 109, 131316, 4284, 6638, 2009, 95.5, 111.5, 175474,
12665, 17213, 1532, 87.5, 84.5, 117568, 4098, 6100, 1436, 83,
91, 118472, 4067.5, 6114, 1651.5, 83, 82, 127308, 4150, 6277,
2028.5, 85.5, 89, 74816, 896.5, 7697, 2698, 84, 92.5, 99431,
1273, 9182.5, 1833.5, 100, 104, 163604, 15582, 12146, 2359, 102,
109, 159301, 17229, 9822.5, 1857, 86, 88, 130319, 4354.5, 6266.5,
1887, 87, 87, 133386, 11639.5, 8931, 2304.5, 86.5, 87, 91022,
1011, 14524, 1353, 84, 88, 114282, 3935, 5944, 1487, 83, 87,
125507, 4138, 5804, 3379, 86.5, 88, 63703.5, 331, 1167, 1778,
87, 83.5, 123988, 4366, 6670, 1862, 94.5, 92, 134174, 4558, 6881,
2388.5, 82, 91.5, 174744, 8570, 10677, 4374, 94, 94, 179579,
12753, 10869, 3747.5, 115, 144.5, 133809, 3710, 5406, 5062, 93.5,
92, 207843, 13220, 6774, 3294, 78, 82.5, 149764, 3774, 5582,
5303, 93, 100, 93479.5, 803, 6709, 2969, 86.5, 101, 149011, 4043,
5407, 5488, 106, 118.5, 191053, 9990.5, 12194, 4308, 89, 85,
143087, 3926.5, 5370.5, 5168, 87, 91.5, 137415, 4028, 5671, 4649.5,
91, 90, 147328, 4102, 5614.5, 7225, 87, 85, 179052, 15612, 16908,
5815.5, 84, 88, 200883, 13229, 11482, 3551, 101, 125, 224012,
20461, 16149.5, 3992, 98, 83, 134744, 3569, 5068, 4817, 97, 92,
142087, 4203, 5678, 5436, 108, 84.5, 195104, 11299, 13246), .Dim = c(6L,
51L), .Dimnames = list(NULL, c("US23502326_253482110017_S01_GE2_1105_Oct12_1_1",
"US23502326_253482110017_S01_GE2_1105_Oct12_2_1", "US23502326_253482110017_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110017_S01_GE2_1105_Oct12_2_3", "US23502326_253482110017_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110027_S01_GE2_1105_Oct12_1_1", "US23502326_253482110027_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110027_S01_GE2_1105_Oct12_1_3", "US23502326_253482110027_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110027_S01_GE2_1105_Oct12_2_1", "US23502326_253482110027_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110027_S01_GE2_1105_Oct12_2_3", "US23502326_253482110027_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110028_S01_GE2_1105_Oct12_1_1", "US23502326_253482110028_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110028_S01_GE2_1105_Oct12_1_4", "US23502326_253482110028_S01_GE2_1105_Oct12_2_1",
"US23502326_253482110028_S01_GE2_1105_Oct12_2_2", "US23502326_253482110028_S01_GE2_1105_Oct12_2_3",
"US23502326_253482110029_S01_GE2_1105_Oct12_1_1", "US23502326_253482110029_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110029_S01_GE2_1105_Oct12_1_3", "US23502326_253482110029_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110029_S01_GE2_1105_Oct12_2_1", "US23502326_253482110029_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110029_S01_GE2_1105_Oct12_2_3", "US23502326_253482110029_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110030_S01_GE2_1105_Oct12_1_1", "US23502326_253482110030_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110030_S01_GE2_1105_Oct12_1_3", "US23502326_253482110030_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110030_S01_GE2_1105_Oct12_2_1", "US23502326_253482110030_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110030_S01_GE2_1105_Oct12_2_3", "US23502326_253482110030_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110031_S01_GE2_1105_Oct12_1_1", "US23502326_253482110031_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110031_S01_GE2_1105_Oct12_1_3", "US23502326_253482110031_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110031_S01_GE2_1105_Oct12_2_1", "US23502326_253482110031_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110031_S01_GE2_1105_Oct12_2_3", "US23502326_253482110031_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110049_S01_GE2_1105_Oct12_1_1", "US23502326_253482110049_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110049_S01_GE2_1105_Oct12_1_3", "US23502326_253482110049_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110049_S01_GE2_1105_Oct12_2_1", "US23502326_253482110049_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110049_S01_GE2_1105_Oct12_2_3", "US23502326_253482110049_S01_GE2_1105_Oct12_2_4"
)))
dput(head(RG$G))
structure(c(2324, 58, 52, 98015, 9800, 5284, 1472, 114, 92.5,
27879, 2296, 3272.5, 3637, 216, 204.5, 34898, 731, 5084, 3466,
77, 74, 32543, 497, 7416, 1344, 79.5, 99, 52753, 2363, 3457,
686, 39, 44.5, 32866, 2937, 4324, 910, 42, 40, 42361, 2780, 4072,
1587, 83.5, 97, 79659, 7667, 10103, 754, 49.5, 44, 23664, 2962,
4166, 1390.5, 136, 156.5, 70132.5, 7914, 4876, 1609, 99, 125,
25923.5, 610, 5125, 1526, 198.5, 157, 94640.5, 10408, 9233, 1060,
42, 37, 70033, 3144, 4355.5, 1465, 89, 91, 99188, 9587, 7547,
743, 61.5, 60, 65888, 3247, 4676.5, 1931.5, 89, 84, 65967, 11226,
7757, 873.5, 56, 66, 20126.5, 3291, 4736, 1339.5, 298, 300, 75324,
6712, 8500, 894, 65, 86, 26341.5, 3132.5, 4647, 2372, 80, 81.5,
73418.5, 5026, 7612, 1564, 70, 73, 77180, 7802.5, 9454, 1315,
90, 85, 20562, 340, 5337, 868.5, 49, 55, 64712.5, 2947, 4260,
798, 46, 48, 52505, 3380, 4663.5, 904, 69.5, 80, 33371.5, 3300,
4997, 813, 73, 81, 29552, 2932, 4632.5, 1696.5, 187, 324, 63647,
6407, 8571.5, 872, 39.5, 52, 24518, 3094, 4387, 752.5, 54, 52,
48299.5, 3221, 4278, 2631, 61, 72, 27229, 513, 5019, 1256.5,
61, 63, 74560, 11016, 9019, 942, 57, 55, 70933.5, 3526.5, 5383,
1457.5, 162, 193.5, 86276, 8154, 12084, 1590, 213, 293, 66871,
6580, 9535, 833.5, 57, 62.5, 36416, 3229, 4600, 2161.5, 53.5,
42.5, 39157.5, 2952, 3977, 3481, 67, 68, 18675, 152, 536, 1977,
57, 55.5, 32861, 2785, 3812, 4739.5, 112.5, 113.5, 104923, 6231,
8198, 1907, 57.5, 69, 76674.5, 3219, 4244, 3879.5, 183.5, 171,
110822, 9582, 8426, 1746, 74.5, 74, 33327, 2774, 4017, 3333,
187, 270.5, 83696, 6616, 7080, 4737, 38, 37, 30041, 429.5, 3970,
3347, 45, 46, 106822, 8003.5, 7137.5, 2431, 32, 35, 32985, 3121,
4179, 2535.5, 28, 34, 36131.5, 3135, 4126, 1929, 42, 65, 47428,
3300, 4626.5, 5371, 54.5, 43.5, 108175, 9983, 6182, 5139.5, 34,
28, 26774, 152, 518, 2621, 48, 32, 44499, 3409, 4643), .Dim = c(6L,
51L), .Dimnames = list(NULL, c("US23502326_253482110017_S01_GE2_1105_Oct12_1_1",
"US23502326_253482110017_S01_GE2_1105_Oct12_2_1", "US23502326_253482110017_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110017_S01_GE2_1105_Oct12_2_3", "US23502326_253482110017_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110027_S01_GE2_1105_Oct12_1_1", "US23502326_253482110027_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110027_S01_GE2_1105_Oct12_1_3", "US23502326_253482110027_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110027_S01_GE2_1105_Oct12_2_1", "US23502326_253482110027_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110027_S01_GE2_1105_Oct12_2_3", "US23502326_253482110027_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110028_S01_GE2_1105_Oct12_1_1", "US23502326_253482110028_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110028_S01_GE2_1105_Oct12_1_4", "US23502326_253482110028_S01_GE2_1105_Oct12_2_1",
"US23502326_253482110028_S01_GE2_1105_Oct12_2_2", "US23502326_253482110028_S01_GE2_1105_Oct12_2_3",
"US23502326_253482110029_S01_GE2_1105_Oct12_1_1", "US23502326_253482110029_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110029_S01_GE2_1105_Oct12_1_3", "US23502326_253482110029_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110029_S01_GE2_1105_Oct12_2_1", "US23502326_253482110029_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110029_S01_GE2_1105_Oct12_2_3", "US23502326_253482110029_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110030_S01_GE2_1105_Oct12_1_1", "US23502326_253482110030_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110030_S01_GE2_1105_Oct12_1_3", "US23502326_253482110030_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110030_S01_GE2_1105_Oct12_2_1", "US23502326_253482110030_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110030_S01_GE2_1105_Oct12_2_3", "US23502326_253482110030_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110031_S01_GE2_1105_Oct12_1_1", "US23502326_253482110031_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110031_S01_GE2_1105_Oct12_1_3", "US23502326_253482110031_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110031_S01_GE2_1105_Oct12_2_1", "US23502326_253482110031_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110031_S01_GE2_1105_Oct12_2_3", "US23502326_253482110031_S01_GE2_1105_Oct12_2_4",
"US23502326_253482110049_S01_GE2_1105_Oct12_1_1", "US23502326_253482110049_S01_GE2_1105_Oct12_1_2",
"US23502326_253482110049_S01_GE2_1105_Oct12_1_3", "US23502326_253482110049_S01_GE2_1105_Oct12_1_4",
"US23502326_253482110049_S01_GE2_1105_Oct12_2_1", "US23502326_253482110049_S01_GE2_1105_Oct12_2_2",
"US23502326_253482110049_S01_GE2_1105_Oct12_2_3", "US23502326_253482110049_S01_GE2_1105_Oct12_2_4"
)))
From the dput you can create an RGList by new("RGList") I know it is to much data but as I previously asked I don't know how to short the output.

The solution that finally I got is doing almost the same:
RG$G <- ifelse(RG$R>R.cut, RG$G, NA)
RG$R <- ifelse(RG$G>G.cut, RG$R, NA)
Deleting this values seems to increase the Fold Change of each gene as already happened comparing the original RG with the RG with deleted values as in the question.

Related

Is there any way to speed up this loop?

I am pretty new to programming and am looking for help to speed up this process. At the current rate the loop will take approximately 24-30 hours.
library(readxl)
## Load data##
A02_pre <- read_excel("C:/Users/Gebruiker/Desktop/Nieuwe map/Mappen participanten/Pre/A02/A02_vivo13_wk2_4mei_90uur.xlsx",
col_types = c("text", "text", "text",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"text", "text", "text", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "text", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "text", "numeric",
"numeric", "numeric", "numeric",
"text", "text"))
A02_pre_hr <- read_excel("C:/Users/Gebruiker/Desktop/Nieuwe map/Mappen participanten/Pre/A02/A02 - 17817.xlsx", )
View(A02_pre_hr)
### Convert string to date time ##
A02_pre$`ns1:Time` = as.POSIXct(A02_pre$`ns1:Time`,
format="%Y-%m-%dT%H:%M:%OSZ",tz ="GMT")
A02_pre_hr$start = as.POSIXct(substr(A02_pre_hr$start,1,nchar(A02_pre_hr$start)-4),
format="%Y-%m-%d %H:%M:%S",tz ="GMT")
## Test dates ##
A02_pre$`ns1:Time`[1] < A02_pre_hr$start[1]
measurementTime <- A02_pre$`ns1:Time`
heartRate <- A02_pre$`ns1:Value4`
**### Run for loop starting here
toCheck = 1:length(measurementTime)
maxj = 0
for(i in 1:(nrow(A02_pre_hr)-1)){
activityStartTime = A02_pre_hr$start[i]
activityEndTime= A02_pre_hr$start[i+1]
heartRateSum = 0
heartRateCount = 0
## Loop over heart rate measurements & assign average to activity
for(j in toCheck[toCheck>maxj]){
if(measurementTime[j]>=activityStartTime & measurementTime[j]<= activityEndTime){
heartRateCount = heartRateCount+1
heartRateSum = heartRateSum + heartRate[j]
}else if(heartRateCount>0){
break;
}
}
averageHeartRate = NA
if(heartRateCount>0){
averageHeartRate = heartRateSum / heartRateCount
}
A02_pre_hr$averageHeartRate[i] = averageHeartRate
print(i)
}**
### Summary ####
by(A02_pre_hr$averageHeartRate, A02_pre_hr$class, summary)
dput(A02_pre)
structure(list(`ns1:Value4` = c(78, 77, 82, 87, 92, 97, 99, 100,
101, 102, 103, 104, 105, 106, 105, 104, 103, 102, 101, 100, 99,
98, 97, 96, 97, 98, 99, 101, 102, 104, 105, 106, 105, 104, 103,
102, 101, 100, 99, 98, 97, 96, 97, 98, 99, 100, 101, 99, 100,
101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 94, 98,
100, 101, 99, 97, 96, 95, 94, 93, 94, 93, 92, 91, 90, 89, 90,
89, 88, 85, 83, 82, 81, 80, 79, 78, 77, 75, 74, 72, 71, 70, 69,
68, 67, 66, 65, 64, 63, 62, 63, 62, 65, 68, 71, 73, 75, 77, 79,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 90, 89, 88, 87, 86,
85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 75, 74, 73, 72,
71, 70, 69, 68, 67, 66, 65, 67, 68, 69, 68, 67, 71, 74, 77, 79,
81, 83, 85, 87, 88, 90, 89, 88, 89, 88, 87, 86, 85, 84, 83, 85,
87, 89, 90, 92, 91, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 107, 111, 114, 117, 117, 116, 115, 116, 117, 118,
119, 120, 119, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108,
107, 106, 105, 104, 103, 102, 100, 99, 98, 97, 96, 95, 94, 93,
92, 90, 88, 89, 90, 91, 93, 94, 95, 96, 95, 94, 93, 92, 91, 90,
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 98, 97, 96, 97, 98,
99, 100, 99, 98, 97, 98, 97, 96, 95, 94, 93, 92, 91, 90, 91,
90, 89, 87, 85, 83, 81, 80, 79, 80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 95, 98, 102, 105, 107, 110, 112, 114, 116, 115, 114,
113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101,
100, 99, 98, 97, 96, 95, 94, 95, 94, 95, 96, 95, 94, 93, 94,
93, 92, 91, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100, 103, 105,
106, 108, 107, 106, 105, 104, 105, 106, 105, 104, 105, 106, 107,
106, 105, 104, 103, 104, 103, 104, 100, 99, 98, 97, 96, 95, 94,
93, 92, 93, 94, 95, 97, 98, 99, 100, 99, 100, 101, 100, 99, 100,
99, 98, 99, 100, 99, 100, 99, 97, 94, 91, 89, 90, 89, 88, 87,
85, 84, 85, 86, 87, 86, 85, 83, 81, 80, 79, 78, 77, 76, 75, 74,
72, 71, 70, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, 83,
84, 85, 86, 85, 84, 85, 86, 87, 88, 87, 88, 89, 90, 91, 92, 91,
89, 88, 87, 88, 89, 90, 95, 96, 95, 94, 93, 92, 91, 90, 89, 88,
87, 86, 85, 84, 85, 86, 87, 86, 84, 85, 86, 87, 86, 87, 86, 85,
84, 83, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75), `ns1:Time` = structure(c(1525439315,
1525439321, 1525439325, 1525439326, 1525439327, 1525439328, 1525439329,
1525439330, 1525439332, 1525439333, 1525439335, 1525439337, 1525439339,
1525439346, 1525439355, 1525439400, 1525439401, 1525439402, 1525439403,
1525439405, 1525439406, 1525439407, 1525439409, 1525439410, 1525439413,
1525439414, 1525439415, 1525439417, 1525439418, 1525439419, 1525439421,
1525439425, 1525439429, 1525439430, 1525439432, 1525439433, 1525439434,
1525439436, 1525439437, 1525439438, 1525439440, 1525439441, 1525439443,
1525439444, 1525439445, 1525439447, 1525439448, 1525439451, 1525439452,
1525439455, 1525439457, 1525439458, 1525439460, 1525439461, 1525439462,
1525439463, 1525439464, 1525439466, 1525439467, 1525439469, 1525439471,
1525439473, 1525439475, 1525439476, 1525439477, 1525439478, 1525439479,
1525439480, 1525439481, 1525439483, 1525439484, 1525439486, 1525439488,
1525439489, 1525439490, 1525439491, 1525439493, 1525439494, 1525439495,
1525439496, 1525439497, 1525439499, 1525439500, 1525439502, 1525439504,
1525439506, 1525439508, 1525439510, 1525439511, 1525439514, 1525439515,
1525439516, 1525439517, 1525439519, 1525439526, 1525439531, 1525439536,
1525439537, 1525439539, 1525439540, 1525439541, 1525439544, 1525439563,
1525439577, 1525439587, 1525439588, 1525439589, 1525439590, 1525439591,
1525439592, 1525439593, 1525439594, 1525439595, 1525439596, 1525439597,
1525439598, 1525439599, 1525439600, 1525439601, 1525439602, 1525439604,
1525439606, 1525439607, 1525439608, 1525439610, 1525439611, 1525439613,
1525439614, 1525439616, 1525439618, 1525439620, 1525439621, 1525439623,
1525439626, 1525439628, 1525439630, 1525439633, 1525439636, 1525439638,
1525439640, 1525439641, 1525439644, 1525439647, 1525439651, 1525439656,
1525439660, 1525439666, 1525439673, 1525439680, 1525439690, 1525439703,
1525439704, 1525439705, 1525439717, 1525439728, 1525439732, 1525439733,
1525439734, 1525439735, 1525439736, 1525439737, 1525439738, 1525439739,
1525439740, 1525439741, 1525439742, 1525439743, 1525439749, 1525439751,
1525439754, 1525439756, 1525439758, 1525439760, 1525439762, 1525439765,
1525439766, 1525439767, 1525439768, 1525439769, 1525439770, 1525439771,
1525439773, 1525439774, 1525439775, 1525439776, 1525439777, 1525439780,
1525439782, 1525439784, 1525439785, 1525439786, 1525439788, 1525439791,
1525439797, 1525439811, 1525439812, 1525439813, 1525439814, 1525439815,
1525439816, 1525439819, 1525439820, 1525439821, 1525439822, 1525439823,
1525439824, 1525439828, 1525439829, 1525439830, 1525439831, 1525439832,
1525439833, 1525439834, 1525439835, 1525439836, 1525439837, 1525439838,
1525439839, 1525439840, 1525439841, 1525439842, 1525439843, 1525439844,
1525439846, 1525439847, 1525439849, 1525439850, 1525439852, 1525439853,
1525439855, 1525439857, 1525439858, 1525439859, 1525439860, 1525439863,
1525439864, 1525439865, 1525439866, 1525439870, 1525439872, 1525439880,
1525439885, 1525439887, 1525439888, 1525439889, 1525439891, 1525439892,
1525439894, 1525439895, 1525439896, 1525439898, 1525439899, 1525439901,
1525439904, 1525439907, 1525439915, 1525439925, 1525439964, 1525439965,
1525439966, 1525439967, 1525439970, 1525439975, 1525439987, 1525440021,
1525440052, 1525440053, 1525440055, 1525440066, 1525440098, 1525440100,
1525440101, 1525440102, 1525440104, 1525440105, 1525440106, 1525440108,
1525440109, 1525440111, 1525440113, 1525440114, 1525440115, 1525440116,
1525440117, 1525440118, 1525440120, 1525440122, 1525440124, 1525440125,
1525440130, 1525440137, 1525440141, 1525440145, 1525440171, 1525440186,
1525440189, 1525440194, 1525440202, 1525440203, 1525440204, 1525440205,
1525440206, 1525440207, 1525440208, 1525440209, 1525440210, 1525440211,
1525440213, 1525440214, 1525440216, 1525440218, 1525440220, 1525440222,
1525440225, 1525440228, 1525440231, 1525440235, 1525440238, 1525440241,
1525440246, 1525440251, 1525440256, 1525440267, 1525440268, 1525440269,
1525440276, 1525440303, 1525440314, 1525440316, 1525440321, 1525440330,
1525440331, 1525440332, 1525440334, 1525440351, 1525440364, 1525440408,
1525440409, 1525440411, 1525440412, 1525440416, 1525440422, 1525440424,
1525440427, 1525440430, 1525440436, 1525440454, 1525440457, 1525440458,
1525440459, 1525440460, 1525440461, 1525440462, 1525440468, 1525440473,
1525440479, 1525440503, 1525440508, 1525440509, 1525440513, 1525440530,
1525440550, 1525440551, 1525440552, 1525440556, 1525440557, 1525440558,
1525440563, 1525440572, 1525440576, 1525440579, 1525440607, 1525440609,
1525440614, 1525440615, 1525440616, 1525440618, 1525440620, 1525440621,
1525440623, 1525440627, 1525440628, 1525440629, 1525440630, 1525440635,
1525440637, 1525440640, 1525440642, 1525440643, 1525440649, 1525440655,
1525440668, 1525440676, 1525440692, 1525440714, 1525440725, 1525440729,
1525440731, 1525440732, 1525440733, 1525440734, 1525440735, 1525440736,
1525440737, 1525440744, 1525440746, 1525440747, 1525440748, 1525440749,
1525440750, 1525440752, 1525440756, 1525440757, 1525440767, 1525440768,
1525440769, 1525440770, 1525440771, 1525440772, 1525440773, 1525440775,
1525440778, 1525440780, 1525440783, 1525440785, 1525440786, 1525440787,
1525440789, 1525440790, 1525440792, 1525440795, 1525440799, 1525440803,
1525440808, 1525440810, 1525440813, 1525440818, 1525440820, 1525440821,
1525440822, 1525440826, 1525440831, 1525440838, 1525440839, 1525440848,
1525440850, 1525440852, 1525440853, 1525440855, 1525440857, 1525440858,
1525440862, 1525440867, 1525440871, 1525440907, 1525440908, 1525440909,
1525440910, 1525440911, 1525440914, 1525440916, 1525440917, 1525440918,
1525440919, 1525440920, 1525440938, 1525440952, 1525440962, 1525440970,
1525440975, 1525441002, 1525441014, 1525441015, 1525441017, 1525441019,
1525441020, 1525441021, 1525441024, 1525441028, 1525441033, 1525441034,
1525441036, 1525441048, 1525441054, 1525441056, 1525441057, 1525441078,
1525441079, 1525441081, 1525441083, 1525441085, 1525441087, 1525441089,
1525441091, 1525441093, 1525441096, 1525441098, 1525441100, 1525441103,
1525441106), tzone = "GMT", class = c("POSIXct", "POSIXt"))), row.names = c(NA,
-500L), class = c("tbl_df", "tbl", "data.frame"))
dput(A02_pre_hr)
structure(list(results_id = c(17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817), start = structure(c(1525439377,
1525439400, 1525439460, 1525439520, 1525439580, 1525439640, 1525439700,
1525439760, 1525439781, 1525439783, 1525439820, 1525439880, 1525439940,
1525439954, 1525439957, 1525440000, 1525440051, 1525440053, 1525440058,
1525440060, 1525440120, 1525440180, 1525440240, 1525440300, 1525440360,
1525440420, 1525440480, 1525440540, 1525440600, 1525440660, 1525440720,
1525440780, 1525440840, 1525440900, 1525440960, 1525441020, 1525441080,
1525441140, 1525441200, 1525441260, 1525441320, 1525441380, 1525441440,
1525441500, 1525441560), tzone = "GMT", class = c("POSIXct",
"POSIXt")), duration = c(22.37, 60, 60, 60, 60, 60, 60, 21.04,
2.65, 36.32, 60, 60, 14.58, 3.07, 42.35, 51.38, 2.46, 4.78, 1.38,
60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60,
60, 60, 60, 60, 60, 60, 60, 60, 60, 60), class = c("standing",
"standing", "standing", "standing", "standing", "standing", "standing",
"standing", "walking", "standing", "standing", "standing", "standing",
"sitting", "standing", "standing", "walking", "standing", "lying",
"lying", "lying", "lying", "lying", "lying", "lying", "lying",
"lying", "lying", "lying", "lying", "lying", "lying", "lying",
"lying", "lying", "lying", "lying", "lying", "lying", "lying",
"lying", "lying", "lying", "lying", "lying"), steps = c(0, 0,
0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0), mi = c(2, 12, 1, 11, 2, 102, 329, 285, 351, 193, 190, 225,
148, 361, 180, 226, 230, 379, 349, 212, 30, 125, 225, 36, 1,
1, 1, 2, 1, 1, 1, 1, 1, 10, 40, 8, 8, 9, 8, 9, 9, 10, 9, 7, 2
), aee = c(16, 21, 15, 20, 16, 64, 173, 151, 313, 107, 106, 123,
86, 129, 101, 123, 246, 197, 448, 272, 38, 160, 289, 46, 1, 1,
1, 2, 1, 1, 1, 1, 1, 12, 51, 10, 10, 11, 10, 11, 11, 12, 11,
9, 2), tee = c(91, 96, 90, 96, 91, 144, 265, 242, 421, 192, 191,
210, 169, 216, 186, 210, 346, 292, 571, 375, 116, 251, 394, 124,
74, 74, 74, 76, 74, 74, 74, 74, 74, 87, 130, 84, 84, 86, 84,
86, 86, 87, 86, 83, 76), met = c(1.145, 1.208, 1.139, 1.201,
1.145, 1.767, 3.18, 2.906, 6.847, 2.334, 2.315, 2.533, 2.054,
2.424, 2.253, 2.539, 5.12, 3.491, 7.275, 4.802, 1.516, 3.231,
5.036, 1.624, 0.992, 0.992, 0.992, 1.01, 0.992, 0.992, 0.992,
0.992, 0.992, 1.155, 1.697, 1.119, 1.119, 1.137, 1.119, 1.137,
1.137, 1.155, 1.137, 1.101, 1.01), counts = c(14, 138, 0, 256,
0, 2059, 7922, 2162, 394, 2952, 2886, 3888, 782, 62, 2163, 3041,
128, 560, 169, 3506, 432, 2024, 3825, 844, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 392, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0), averageHeartRate = c(104,
100.236842105263, 88.6904761904762, 64.9, 81.1944444444444, 70,
80.4347826086957, 90.1333333333333, 97, 108.285714285714, 103.775,
93.2941176470588, NA, NA, 97.7142857142857, 100, 98.5, 97.5,
NA, 89.5, 83, 105.833333333333, 99.5, 94.5555555555556, 91.8333333333333,
100.4, 104.8, 104.7, 97, 99, 87.4642857142857, 76.4090909090909,
87.5, 91.3076923076923, 88, 85.6923076923077, 82.0714285714286,
91.2, 86.6428571428571, 90.6363636363636, 105.142857142857, 95.5,
94.1, 102.5, 93.8333333333333)), row.names = c(NA, -45L), class = c("tbl_df",
"tbl", "data.frame"))
I'm trying to get an average heart rate out of every activity. The
heart rate has been measured with a sports watch and the activities
(sitting, lying, walking etc) have been measured with an accelerometer
(which in turn estimates what kind of activity was being done).
You could use a data.table non-qui join with by = .EACHI. That should be quite fast.
library(data.table)
setDT(A02_pre_hr)
setDT(A02_pre)
A02_pre_hr[, start := as.POSIXct(start)]
A02_pre_hr[, end := start + duration]
A02_pre[, time := as.POSIXct(`ns1:Time`)]
A02_pre_hr[, averageHeartRate1 := A02_pre[A02_pre_hr, mean(`ns1:Value4`, na.rm = TRUE),
on = c("time >= start", "time <= end"), by = .EACHI]$V1]
# results_id start duration class steps mi aee tee met counts averageHeartRate end averageHeartRate1
# 1: 17817 2018-05-04 13:09:37 22.37 standing 0 2 16 91 1.145 14 104.00000 2018-05-04 13:09:59 NaN
# 2: 17817 2018-05-04 13:10:00 60.00 standing 0 12 21 96 1.208 138 100.23684 2018-05-04 13:11:00 100.23684
# 3: 17817 2018-05-04 13:11:00 60.00 standing 0 1 15 90 1.139 0 88.69048 2018-05-04 13:12:00 88.69048
# 4: 17817 2018-05-04 13:12:00 60.00 standing 0 11 20 96 1.201 256 64.90000 2018-05-04 13:13:00 64.90000
# 5: 17817 2018-05-04 13:13:00 60.00 standing 0 2 16 91 1.145 0 81.19444 2018-05-04 13:14:00 81.19444
# 6: 17817 2018-05-04 13:14:00 60.00 standing 0 102 64 144 1.767 2059 70.00000 2018-05-04 13:15:00 70.00000
# 7: 17817 2018-05-04 13:15:00 60.00 standing 0 329 173 265 3.180 7922 80.43478 2018-05-04 13:16:00 80.43478
# ....
The first value doesn't match because of the subseconds in the duration.

How to reorder day of year so that seasons line up

I am working with seasonal data in Australia that spans from their Spring to Fall. I would like to have a column with day of year. However, when I use the yday function from the lubridate, I receive a distribution like this:
The issue here is that the Australian summer (peak time for data collection) goes through the new year. If I tried to add this variable into the model, it would produce incorrect results.
So my question is:
How can I get a seasonally appropriate 'day of year' column for this dataset
I have included some data here:
structure(list(Date = structure(c(11323, 11323, 11323, 11323,
12784, 11324, 12785, 11324, 12419, 12785, 12786, 12786, 13151,
13151, 13151, 12787, 12421, 15343, 15709, 12787, 14614, 15344,
11327, 15344, 14614, 13154, 15711, 15711, 11328, 13154, 16442,
15712, 15712, 16442, 16442, 13886, 16443, 16443, 16443, 16443,
16444, 16444, 13887, 13157, 13157, 13158, 13158, 11697, 16445,
13158, 13889, 16446, 11698, 15716, 11333, 16447, 13525, 13525,
13160, 15351, 11335, 12430, 14257, 13161, 12796, 13162, 12066,
13162, 13892, 13162, 13893, 16085, 14989, 16815, 16085, 16086,
13164, 16086, 17182, 13894, 17183, 15356, 17183, 13165, 11704,
14992, 13166, 14262, 17184, 12435, 11706, 17185, 14628, 14993,
14628, 14629, 14629, 14994, 13168, 16455, 16456, 12804, 16456,
16456, 13899, 12805, 14996, 14996, 16822, 16457, 13171, 16458,
14997, 12806, 14997, 14998, 13172, 13172, 13172, 14998, 13173,
13173, 12808, 12808, 13173, 17192, 13904, 13174, 13174, 13174,
14271, 13175, 13175, 15001, 17193, 12445, 17194, 13176, 12445,
14272, 16464, 16464, 15734, 13907, 14638, 14639, 13178, 13178,
16465, 14639, 16466, 13179, 13179, 16101, 13179, 13910, 12449,
16467, 13910, 16467, 13181, 15372, 13181, 14277, 16468, 14278,
13182, 14278, 12451, 16469, 16470, 13183, 14279, 16835, 14644,
15010, 13549, 14645, 12819, 12819, 17203, 17203, 12454, 12820,
17203, 12821, 15012, 13916, 13916, 12821, 13917, 15013, 15013,
13187, 13917, 13918, 13553, 17206, 14284, 12457, 16476, 14285,
15380, 14285, 16476, 12825, 12459, 12459, 13920, 16477, 13921,
12460, 16478, 12460, 14287, 16114, 14288, 16844, 11731, 16479,
16480, 11367, 11367, 11367, 16845, 16481, 16481, 16846, 16846,
12829, 12830, 11003, 16482, 13925, 11369, 12465, 16483, 16483,
16483, 14657, 17215, 11371, 14658, 16484, 16484, 17216, 16485,
12467, 16850, 11737, 14295, 12834, 12468, 14295, 16851, 13200,
13200, 16487, 11374, 15026, 11375, 16488, 16488, 12836, 16488,
11376, 14663, 13202, 14663, 14663, 14664, 11742, 15029, 11377,
13203, 16491, 16491, 16491, 13569, 13204, 13205, 14301, 11744,
16492, 13935, 16493, 11380, 16493, 11014, 13936, 16494, 13937,
13937, 13572, 16494, 13208, 11382, 13938, 11016, 13208, 11748,
14670, 16496, 14670, 14670, 11384, 14671, 14306, 14671, 12479,
14672, 16498, 16498, 14672, 14672, 14673, 13942, 12481, 13942,
12481, 14309, 16500, 12482, 13943, 13943, 12849, 12483, 15771,
13944, 16501, 16867, 16502, 13945, 12484, 15406, 17234, 16868,
11024, 16503, 17234, 17235, 17235, 17235, 17235, 16869, 17236,
13948, 13948, 13948, 17236, 15045, 15410, 13949, 16141, 13219,
15046, 12489, 15411, 13950, 13950, 11395, 11395, 11395, 12856,
11395, 15779, 15048, 16874, 12857, 16874, 16875, 16510, 16510,
15049, 15049, 14685, 12859, 16511, 15050, 16146, 16512, 14686,
12860, 12860, 12860, 16513, 16878, 14322, 12861, 11400, 15784,
16514, 15784, 13227, 12862, 16515, 12497, 16515, 16515, 14689,
13594, 14690, 12498, 12864, 12864, 11404, 13595, 11404, 12865,
12499, 12500, 12866, 14327, 16518, 16518, 14693, 14328, 14693,
16519, 16519, 12502, 16520, 14329, 14329, 14329, 12503, 11042,
16521, 11042, 13234, 11409, 12504, 14331, 12504, 12504, 15427,
15427, 13601, 15427, 11775, 13602, 16889, 17255, 12506, 12506,
16160, 12873, 15429, 14334, 12507, 12508, 12508, 11047, 13969,
12508, 13970, 11414, 14336, 13605, 13970, 12876, 17259, 16893,
16163, 13606, 13972, 16894, 16894, 11416, 13607, 13973, 12878,
13243, 11417, 13973, 17262, 12513, 13974, 12513, 12513, 16897,
12514, 12514, 16897, 11419, 13976, 16898, 16898, 13976, 16898,
16169, 12151, 15804, 11421, 16899, 16170, 16535, 12883, 16170,
16535, 11057, 11057, 16171, 13979, 16171, 13980, 13250, 14711,
16537, 13980, 14712, 13981, 15808, 13981, 14712, 14713, 14713,
14713, 16174, 14713, 16905, 15444, 16905, 14349, 14349, 13619,
14715, 12523, 16906, 14350, 14716, 13620, 15446, 12524, 12524,
13621, 13621, 13621, 14352, 12525, 12526, 12526, 12526, 12526,
14718, 12527, 11066, 16910, 12527, 13623, 16911, 12528, 16911,
14720, 16911, 14721, 12529, 13990, 11068, 13990, 14357, 12530,
14722, 14357, 13626, 13627, 12531, 13627, 13992, 13992, 11071,
13993, 11071, 11071, 14359, 13994, 11072, 13994, 14360, 13994,
14361, 11073, 14361, 14361, 14361, 16188, 16188, 14362, 14362,
14362, 11075, 13997, 14363, 14363, 14363, 14364, 16190, 14364,
16190, 16190, 14365, 16191, 16191, 11808, 16191, 14366, 14366,
16192, 11078, 14366, 16193, 16193, 16193, 16193, 16193, 16194,
16194, 16194, 16194, 16194, 12542, 16195, 16195, 12542, 12542,
12543, 12543, 12543, 12543, 16196, 14005, 14371, 16197, 14005,
14005, 12545, 14006, 12545, 14006, 12545, 14007, 14007, 12546,
12546, 12546, 14739, 14008, 12547, 12547, 14008, 14009, 14009,
14740, 12548, 15470, 14741, 14741, 15471, 14741, 14741, 14742,
14742, 14742, 16203, 14742, 14743, 14743, 14743, 14743, 14743,
16205, 16205, 13283, 16205, 16205, 16206, 13284, 13284, 16206,
13284, 16207, 16207, 16207, 16207, 15476, 15477, 15477, 15477,
15477, 15477, 15478, 15478, 15478, 14748, 15478, 15479, 14749,
11827, 14749, 14749, 11828, 11828, 14750, 11828, 14750, 16943,
16943, 16943, 16943, 16943, 11100, 11100, 11100, 11100, 11100,
16945, 16945, 16945, 16945, 16945, 16946, 16946, 16946, 16946,
16946, 16947, 16947, 16947, 16947, 16947, 16948, 16948, 16948,
16948, 16948, 16949, 16949, 16949, 16949, 16949, 16314, 16314,
14853, 16314, 16314, 16315, 16315, 14489, 14489, 14489, 16316,
16316, 16316, 14855, 14490, 16317, 13760, 16317, 16317, 16317,
16318, 16318, 13761, 14126, 13761, 13762, 15223, 13762, 16319,
16319, 12667, 14859, 14859, 17050, 12667, 16321, 11207, 11207,
11207, 11207, 11208, 11208, 14496, 13400, 13400, 16323, 14497,
16323, 13401, 14497, 14498, 16324, 14498, 16324, 13402, 16325,
15229, 11211, 15229, 13403, 15230, 15230, 15961, 17056, 15230,
14866, 15231, 13040, 15231, 15962, 13771, 14867, 14502, 15963,
11580, 14868, 14868, 15233, 16694, 16694, 15965, 14869, 15965,
13043, 14504, 14139, 16696, 12678, 14505, 15966, 14871, 13045,
16697, 16697, 14140, 14872, 11219, 11219, 14872, 13411, 13412,
11220, 15969, 14873, 14873, 14509, 14874, 14509, 14874, 12682,
14875, 12683, 14875, 14875, 14875, 12684, 11223, 14511, 14511,
14876, 14877, 12685, 14877, 14877, 12685, 12686, 16339, 14878,
14147, 14878, 16340, 14879, 14879, 14879, 14879, 11227, 11227,
14880, 14880, 14880, 14881, 14881, 16342, 14881, 14881, 16343,
14882, 14882, 14882, 14882, 11230, 14883, 12691, 14883, 12691,
16345, 14153, 16710, 14884, 14884, 14154, 15615, 14885, 12693,
11232, 14886, 14155, 14155, 14886, 14886, 14887, 14887, 14887,
14887, 14887, 14888, 14888, 14888, 12696, 14523, 14889, 16350,
14889, 15619, 14889, 14890, 14525, 14890, 14525, 14525, 14526,
14891, 14891, 14891, 14526, 14892, 14892, 16353, 16353, 12335,
14893, 14893, 14893, 14893, 14893, 13798, 17085, 14894, 17085,
13068, 14895, 13069, 14895, 14895, 14895, 17087, 14896, 17087,
14896, 12339, 17088, 16358, 16358, 14532, 16358, 14898, 12706,
14898, 14898, 14898, 16360, 14899, 14899, 11246, 14899, 14900,
12708, 14900, 14900, 14900, 14901, 14901, 14901, 16362, 14901,
14902, 14902, 14902, 14537, 14902, 14903, 14903, 14903, 14903,
12711, 14904, 12712, 14904, 12712, 14904, 15635, 14905, 11252,
14905, 11252, 14906, 14906, 12349, 14906, 14906, 14542, 14907,
14542, 14542, 16368, 16369, 14908, 14908, 14177, 16369, 16370,
14909, 14909, 12352, 16370, 14910, 17101, 14910, 14545, 14910,
14911, 11258, 16737, 12719, 14911, 16373, 16373, 14912, 14912,
12720, 14913, 14913, 14913, 14913, 11260, 14914, 16375, 11992,
11627, 14914, 14915, 14915, 14915, 14550, 15280, 14916, 13820,
14551, 14916, 14916, 14917, 14917, 14917, 14917, 14917, 11265,
14553, 16014, 14918, 12726, 11266, 14919, 11266, 16380, 14919,
14920, 12728, 12363, 12363, 14920, 14921, 14921, 17112, 14921,
14921, 14922, 14922, 14922, 11269, 14922, 14923, 14923, 14923,
16384, 14923, 14924, 16385, 14924, 14924, 11271, 16386, 14194,
14925, 16386, 14925, 14926, 16387, 13465, 14926, 11273, 11640,
12005, 14196, 14927, 14196, 14197, 12736, 14928, 14928, 14928,
12737, 13833, 14929, 14929, 11642, 14930, 14930, 14930, 14930,
14930, 17122, 12739, 13470, 13470, 12374, 14932, 12375, 14932,
13471, 13471, 11280, 14933, 14933, 14933, 14202, 14934, 14934,
14203, 14934, 14934, 14204, 14935, 14935, 14935, 14935, 12744,
14936, 14936, 14936, 14936, 16398, 11284, 16398, 16398, 16398,
11285, 14938, 16764, 14938, 12381, 16400, 16400, 12747, 12382,
14208, 14209, 11287, 14209, 16401, 13844, 16402, 15671, 14210,
16402, 12749, 12750, 13846, 16403, 16403, 14211, 15673, 15673,
16404, 11656, 16404, 17135, 17135, 16405, 16405, 17135, 14580,
16771, 12753, 12388, 14580, 13485, 17137, 14581, 14581, 12389,
16773, 16773, 15677, 13486, 11294, 12391, 16044, 14217, 14217,
13122, 12757, 13853, 14218, 17140, 14949, 16776, 17141, 17141,
16046, 14950, 15681, 12759, 15681, 16412, 12759, 14587, 13126,
13126, 16413, 16413, 16414, 16414, 16414, 13492, 16414, 16415,
15684, 16415, 16415, 16415, 16416, 14224, 16416, 14224, 14590,
14591, 14956, 12764, 14591, 16052, 14957, 12400, 11304, 11304,
12765, 14958, 14593, 11305, 16784, 14958, 12402, 12402, 12402,
12402, 12767, 14960, 14595, 14595, 13134, 16421, 12769, 12404,
12769, 14230, 14961, 12770, 12770, 12770, 15327, 12770, 14232,
14963, 12406, 17154, 14232, 17155, 14233, 14233, 17155, 17155,
17156, 17156, 14234, 17156, 12408, 12774, 17157, 12774, 17157,
17157, 11314, 12775, 17158, 12775, 12775, 11315, 11315, 11315,
11315, 11315, 11684, 11684, 13510, 11684, 11684, 13511, 13511,
13511, 11319, 11685, 11320, 12781, 11320, 13512, 12781, 11321,
11321, 11321, 11321, 11321, 11322, 11322, 11322, 11322, 11322
), class = "Date"), DOY = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14,
14, 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17,
17, 17, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20,
20, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23,
24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27,
27, 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30,
30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33, 33, 33,
33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36, 36,
36, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39,
40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 43,
43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 46, 46,
46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 49, 49, 49,
49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52,
52, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55,
56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 58, 58, 58, 58, 58, 59,
59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 62, 62,
62, 62, 62, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 65, 65, 65,
65, 65, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 68, 68, 68, 68,
68, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71,
72, 72, 72, 72, 72, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 75,
75, 75, 75, 75, 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 78, 78,
78, 78, 78, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80, 81, 81, 81,
81, 81, 82, 82, 82, 82, 82, 83, 83, 83, 83, 83, 84, 84, 84, 84,
84, 85, 85, 85, 85, 85, 86, 86, 86, 86, 86, 87, 87, 87, 87, 87,
88, 88, 88, 88, 88, 89, 89, 89, 89, 89, 90, 90, 90, 90, 90, 91,
91, 91, 91, 91, 92, 92, 92, 92, 92, 93, 93, 93, 93, 93, 94, 94,
94, 94, 94, 95, 95, 95, 95, 95, 96, 96, 96, 96, 96, 97, 97, 97,
97, 97, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99, 100, 100, 100,
100, 100, 101, 101, 101, 101, 101, 102, 102, 102, 102, 102, 103,
103, 103, 103, 103, 104, 104, 104, 104, 104, 105, 105, 105, 105,
105, 106, 106, 106, 106, 106, 107, 107, 107, 107, 107, 108, 108,
108, 108, 108, 109, 109, 109, 109, 109, 110, 110, 110, 110, 110,
111, 111, 111, 111, 111, 112, 112, 112, 112, 112, 113, 113, 113,
113, 113, 114, 114, 114, 114, 114, 115, 115, 115, 115, 115, 116,
116, 116, 116, 116, 117, 117, 117, 117, 117, 118, 118, 118, 118,
118, 119, 119, 119, 119, 119, 120, 120, 120, 120, 120, 121, 121,
121, 121, 121, 122, 122, 122, 122, 122, 123, 123, 123, 123, 123,
124, 124, 124, 124, 124, 125, 125, 125, 125, 125, 126, 126, 126,
126, 126, 127, 127, 127, 127, 127, 128, 128, 128, 128, 128, 129,
129, 129, 129, 129, 130, 130, 130, 130, 130, 131, 131, 131, 131,
131, 132, 132, 132, 132, 132, 133, 133, 133, 133, 133, 134, 134,
134, 134, 134, 135, 135, 135, 135, 135, 136, 136, 136, 136, 136,
137, 137, 137, 137, 137, 138, 138, 138, 138, 138, 139, 139, 139,
139, 139, 140, 140, 140, 140, 140, 141, 141, 141, 141, 141, 143,
143, 143, 143, 143, 144, 144, 144, 144, 144, 145, 145, 145, 145,
145, 146, 146, 146, 146, 146, 147, 147, 147, 147, 147, 148, 148,
148, 148, 148, 149, 149, 149, 149, 149, 244, 244, 244, 244, 244,
245, 245, 245, 245, 245, 246, 246, 246, 246, 246, 247, 247, 247,
247, 247, 248, 248, 248, 248, 248, 249, 249, 249, 249, 249, 250,
250, 250, 250, 250, 251, 251, 251, 251, 251, 252, 252, 252, 252,
252, 253, 253, 253, 253, 253, 254, 254, 254, 254, 254, 255, 255,
255, 255, 255, 256, 256, 256, 256, 256, 257, 257, 257, 257, 257,
258, 258, 258, 258, 258, 259, 259, 259, 259, 259, 260, 260, 260,
260, 260, 261, 261, 261, 261, 261, 262, 262, 262, 262, 262, 263,
263, 263, 263, 263, 264, 264, 264, 264, 264, 265, 265, 265, 265,
265, 266, 266, 266, 266, 266, 267, 267, 267, 267, 267, 268, 268,
268, 268, 268, 269, 269, 269, 269, 269, 270, 270, 270, 270, 270,
271, 271, 271, 271, 271, 272, 272, 272, 272, 272, 273, 273, 273,
273, 273, 274, 274, 274, 274, 274, 275, 275, 275, 275, 275, 276,
276, 276, 276, 276, 277, 277, 277, 277, 277, 278, 278, 278, 278,
278, 279, 279, 279, 279, 279, 280, 280, 280, 280, 280, 281, 281,
281, 281, 281, 282, 282, 282, 282, 282, 283, 283, 283, 283, 283,
284, 284, 284, 284, 284, 285, 285, 285, 285, 285, 286, 286, 286,
286, 286, 287, 287, 287, 287, 287, 288, 288, 288, 288, 288, 289,
289, 289, 289, 289, 290, 290, 290, 290, 290, 291, 291, 291, 291,
291, 292, 292, 292, 292, 292, 293, 293, 293, 293, 293, 294, 294,
294, 294, 294, 295, 295, 295, 295, 295, 296, 296, 296, 296, 296,
297, 297, 297, 297, 297, 298, 298, 298, 298, 298, 299, 299, 299,
299, 299, 300, 300, 300, 300, 300, 301, 301, 301, 301, 301, 302,
302, 302, 302, 302, 303, 303, 303, 303, 303, 304, 304, 304, 304,
304, 305, 305, 305, 305, 305, 306, 306, 306, 306, 306, 307, 307,
307, 307, 307, 308, 308, 308, 308, 308, 309, 309, 309, 309, 309,
310, 310, 310, 310, 310, 311, 311, 311, 311, 311, 312, 312, 312,
312, 312, 313, 313, 313, 313, 313, 314, 314, 314, 314, 314, 315,
315, 315, 315, 315, 316, 316, 316, 316, 316, 317, 317, 317, 317,
317, 318, 318, 318, 318, 318, 319, 319, 319, 319, 319, 320, 320,
320, 320, 320, 321, 321, 321, 321, 321, 322, 322, 322, 322, 322,
323, 323, 323, 323, 323, 324, 324, 324, 324, 324, 325, 325, 325,
325, 325, 326, 326, 326, 326, 326, 327, 327, 327, 327, 327, 328,
328, 328, 328, 328, 329, 329, 329, 329, 329, 330, 330, 330, 330,
330, 331, 331, 331, 331, 331, 332, 332, 332, 332, 332, 333, 333,
333, 333, 333, 334, 334, 334, 334, 334, 335, 335, 335, 335, 335,
336, 336, 336, 336, 336, 337, 337, 337, 337, 337, 338, 338, 338,
338, 338, 339, 339, 339, 339, 339, 340, 340, 340, 340, 340, 341,
341, 341, 341, 341, 342, 342, 342, 342, 342, 343, 343, 343, 343,
343, 344, 344, 344, 344, 344, 345, 345, 345, 345, 345, 346, 346,
346, 346, 346, 347, 347, 347, 347, 347, 348, 348, 348, 348, 348,
349, 349, 349, 349, 349, 350, 350, 350, 350, 350, 351, 351, 351,
351, 351, 352, 352, 352, 352, 352, 353, 353, 353, 353, 353, 354,
354, 354, 354, 354, 355, 355, 355, 355, 355, 356, 356, 356, 356,
356, 357, 357, 357, 357, 357, 358, 358, 358, 358, 358, 359, 359,
359, 359, 359, 362, 362, 362, 362, 362, 363, 363, 363, 363, 363,
364, 364, 364, 364, 364, 365, 365, 365, 365, 365, 366, 366, 366,
366, 366)), row.names = c(NA, -1345L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), groups = structure(list(DOY = c(1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
139, 140, 141, 143, 144, 145, 146, 147, 148, 149, 244, 245, 246,
247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259,
260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272,
273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285,
286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298,
299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311,
312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324,
325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337,
338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350,
351, 352, 353, 354, 355, 356, 357, 358, 359, 362, 363, 364, 365,
366), .rows = list(1:5, 6:10, 11:15, 16:20, 21:25, 26:30, 31:35,
36:40, 41:45, 46:50, 51:55, 56:60, 61:65, 66:70, 71:75, 76:80,
81:85, 86:90, 91:95, 96:100, 101:105, 106:110, 111:115, 116:120,
121:125, 126:130, 131:135, 136:140, 141:145, 146:150, 151:155,
156:160, 161:165, 166:170, 171:175, 176:180, 181:185, 186:190,
191:195, 196:200, 201:205, 206:210, 211:215, 216:220, 221:225,
226:230, 231:235, 236:240, 241:245, 246:250, 251:255, 256:260,
261:265, 266:270, 271:275, 276:280, 281:285, 286:290, 291:295,
296:300, 301:305, 306:310, 311:315, 316:320, 321:325, 326:330,
331:335, 336:340, 341:345, 346:350, 351:355, 356:360, 361:365,
366:370, 371:375, 376:380, 381:385, 386:390, 391:395, 396:400,
401:405, 406:410, 411:415, 416:420, 421:425, 426:430, 431:435,
436:440, 441:445, 446:450, 451:455, 456:460, 461:465, 466:470,
471:475, 476:480, 481:485, 486:490, 491:495, 496:500, 501:505,
506:510, 511:515, 516:520, 521:525, 526:530, 531:535, 536:540,
541:545, 546:550, 551:555, 556:560, 561:565, 566:570, 571:575,
576:580, 581:585, 586:590, 591:595, 596:600, 601:605, 606:610,
611:615, 616:620, 621:625, 626:630, 631:635, 636:640, 641:645,
646:650, 651:655, 656:660, 661:665, 666:670, 671:675, 676:680,
681:685, 686:690, 691:695, 696:700, 701:705, 706:710, 711:715,
716:720, 721:725, 726:730, 731:735, 736:740, 741:745, 746:750,
751:755, 756:760, 761:765, 766:770, 771:775, 776:780, 781:785,
786:790, 791:795, 796:800, 801:805, 806:810, 811:815, 816:820,
821:825, 826:830, 831:835, 836:840, 841:845, 846:850, 851:855,
856:860, 861:865, 866:870, 871:875, 876:880, 881:885, 886:890,
891:895, 896:900, 901:905, 906:910, 911:915, 916:920, 921:925,
926:930, 931:935, 936:940, 941:945, 946:950, 951:955, 956:960,
961:965, 966:970, 971:975, 976:980, 981:985, 986:990, 991:995,
996:1000, 1001:1005, 1006:1010, 1011:1015, 1016:1020, 1021:1025,
1026:1030, 1031:1035, 1036:1040, 1041:1045, 1046:1050, 1051:1055,
1056:1060, 1061:1065, 1066:1070, 1071:1075, 1076:1080, 1081:1085,
1086:1090, 1091:1095, 1096:1100, 1101:1105, 1106:1110, 1111:1115,
1116:1120, 1121:1125, 1126:1130, 1131:1135, 1136:1140, 1141:1145,
1146:1150, 1151:1155, 1156:1160, 1161:1165, 1166:1170, 1171:1175,
1176:1180, 1181:1185, 1186:1190, 1191:1195, 1196:1200, 1201:1205,
1206:1210, 1211:1215, 1216:1220, 1221:1225, 1226:1230, 1231:1235,
1236:1240, 1241:1245, 1246:1250, 1251:1255, 1256:1260, 1261:1265,
1266:1270, 1271:1275, 1276:1280, 1281:1285, 1286:1290, 1291:1295,
1296:1300, 1301:1305, 1306:1310, 1311:1315, 1316:1320, 1321:1325,
1326:1330, 1331:1335, 1336:1340, 1341:1345)), row.names = c(NA,
-269L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
The sample data you gave was not very illustrative, since it contained one instance each of the same day each year. We'll create some data that is perhaps more representative:
library(ggplot2)
set.seed(69)
dates <- as.Date("2015-01-01") +
lubridate::days(round(rnorm(5000, 0, 30))) +
lubridate::years(sample(0:5, 5000, TRUE))
df <- data.frame(Date = dates, DOY = lubridate::yday(dates))
ggplot(df, aes(DOY)) + geom_histogram(binwidth = 1)
The way to get rid of this gap is simply to add 6 months and take the result modulo 366:
ggplot(df, aes((DOY + 183) %% 366)) +
geom_histogram(binwidth = 1) +
lims(x = c(0, 366)) +
labs(x = "DOY")
Created on 2020-08-02 by the reprex package (v0.3.0)

How to find and plot correlated variables in R?

I'm reviewing factors related to cancer and hoping to find how they're related to one another. After I input my .xslx file into R, I'm at a loss how to use that list of data to find the z-scores for each measurement and then using that matrix Z of z-scores of X to compute the covariance matrix of A using cov(Z). How do I plot and analyze entries of A (which tell me how correlated the measurements in X are)?
This is an example of the plot I'm trying to achieve with my factors (UrbanPop, Rape, Assault, and Murder are factors of that plot -- you can ignore the states plotted).
Thank you!
The factors I'm reviewing are Age, BMI, Glucose, Insulin, HOMA, Leptin, Adiponectin, Resistin, MCP.1
This is my data -- I used dput(mydata)
structure(list(Age = c(48, 83, 82, 68, 86, 49, 89, 76, 73, 75,
34, 29, 25, 24, 38, 44, 47, 61, 64, 32, 36, 34, 29, 35, 54, 45,
50, 66, 35, 36, 66, 53, 28, 43, 51, 67, 66, 69, 60, 77, 76, 76,
75, 69, 71, 66, 75, 78, 69, 85, 76, 77, 45, 45, 49, 34, 42, 68,
51, 62, 38, 69, 49, 51, 59, 45, 54, 64, 46, 44, 45, 44, 51, 72,
46, 43, 55, 43, 86, 41, 59, 81, 48, 71, 42, 65, 48, 85, 48, 58,
40, 82, 52, 49, 60, 49, 44, 40, 71, 69, 74, 66, 65, 72, 57, 73,
45, 46, 68, 75, 54, 45, 62, 65, 72, 86), BMI = c(23.5, 20.69049,
23.12467, 21.36752, 21.11111, 22.85446, 22.7, 23.8, 22, 23, 21.47,
23.01, 22.86, 18.67, 23.34, 20.76, 22.03, 32.03896, 34.52972,
36.51264, 28.57668, 31.97501, 32.27079, 30.27682, 30.48316, 37.03561,
38.57876, 31.44654, 35.25076, 34.17489, 36.21228, 36.79017, 35.85581,
34.42217, 27.68878, 29.60677, 31.23859, 35.0927, 26.34929, 35.58793,
29.21841, 27.2, 27.3, 32.5, 30.3, 27.7, 25.7, 25.3, 29.4, 26.6,
27.1, 25.9, 21.30395, 20.83, 20.95661, 24.24242, 21.35991, 21.08281,
19.13265, 22.65625, 22.49964, 21.51386, 21.36752, 22.89282, 22.83288,
23.1405, 24.21875, 22.22222, 20.83, 19.56, 20.26, 24.74, 18.37,
23.62, 22.21, 26.5625, 31.97501, 31.25, 26.66667, 26.67276, 28.67263,
31.64037, 32.46191, 25.5102, 29.29688, 29.66655, 28.125, 27.68878,
31.25, 29.15452, 30.83653, 31.21748, 30.80125, 32.46191, 31.23141,
29.77778, 27.88762, 27.63605, 27.91552, 28.44444, 28.65014, 26.5625,
30.91558, 29.13632, 34.83815, 37.10938, 29.38476, 33.18, 35.56,
30.48, 36.05, 26.85, 26.84, 32.05, 25.59, 27.18), Glucose = c(70,
92, 91, 77, 92, 92, 77, 118, 97, 83, 78, 82, 82, 88, 75, 86,
84, 85, 95, 87, 86, 87, 84, 84, 90, 83, 106, 90, 90, 80, 101,
101, 87, 89, 77, 79, 82, 101, 103, 76, 83, 94, 85, 93, 102, 90,
94, 60, 89, 96, 110, 85, 102, 74, 94, 92, 93, 102, 93, 92, 95,
112, 78, 103, 98, 116, 86, 98, 88, 114, 92, 106, 105, 105, 86,
101, 92, 103, 201, 97, 77, 100, 99, 112, 98, 85, 90, 196, 199,
139, 128, 100, 87, 134, 131, 70, 99, 103, 104, 108, 88, 89, 97,
83, 95, 134, 90, 92, 131, 152, 119, 92, 100, 97, 82, 138), Insulin = c(2.707,
3.115, 4.498, 3.226, 3.549, 3.226, 4.69, 6.47, 3.35, 4.952, 3.469,
5.663, 4.09, 6.107, 5.782, 7.553, 2.869, 18.077, 4.427, 14.026,
4.345, 4.53, 5.81, 4.376, 5.537, 6.76, 6.703, 9.245, 6.817, 6.59,
15.533, 10.175, 8.576, 23.194, 3.855, 5.819, 4.181, 5.646, 5.138,
3.881, 5.376, 14.07, 5.197, 5.43, 8.34, 6.042, 8.079, 3.508,
10.704, 4.462, 26.211, 4.58, 13.852, 4.56, 12.305, 21.699, 2.999,
6.2, 4.364, 3.482, 5.261, 6.683, 2.64, 2.74, 6.862, 4.902, 3.73,
5.7, 3.42, 15.89, 3.44, 58.46, 6.03, 4.42, 36.94, 10.555, 16.635,
4.328, 41.611, 22.033, 3.188, 9.669, 28.677, 10.395, 4.172, 14.649,
2.54, 51.814, 12.162, 16.582, 41.894, 18.077, 30.212, 24.887,
30.13, 8.396, 9.208, 2.432, 18.2, 8.808, 3.012, 6.524, 10.491,
10.949, 12.548, 5.636, 4.713, 5.75, 8.15, 7.01, 11.91, 3.33,
4.53, 5.73, 2.82, 19.91), HOMA = c(0.467409, 0.706897, 1.009651,
0.612725, 0.805386, 0.732087, 0.890787, 1.883201, 0.801543, 1.013839,
0.667436, 1.145436, 0.827271, 1.33, 1.06967, 1.6, 0.59, 3.790144,
1.037394, 3.00998, 0.921719, 0.972138, 1.203832, 0.906707, 1.229214,
1.383997, 1.752611, 2.05239, 1.513374, 1.300427, 3.869788, 2.534932,
1.84041, 5.091856, 0.732193, 1.133929, 0.845677, 1.406607, 1.305395,
0.727558, 1.100646, 3.262364, 1.089638, 1.245642, 2.098344, 1.341324,
1.873251, 0.519184, 2.349885, 1.056602, 7.111918, 0.960273, 3.485163,
0.832352, 2.853119, 4.924226, 0.687971, 1.55992, 1.001102, 0.790182,
1.232828, 1.84629, 0.507936, 0.696143, 1.658774, 1.402626, 0.791257,
1.37788, 0.742368, 4.468268, 0.780651, 15.28534, 1.56177, 1.14478,
7.836205, 2.629602, 3.775036, 1.099601, 20.63073, 5.271762, 0.605507,
2.38502, 7.002923, 2.871792, 1.008511, 3.071407, 0.56388, 25.05034,
5.96992, 5.685415, 13.22733, 4.458993, 6.483495, 8.225983, 9.736007,
1.449709, 2.248594, 0.61789, 4.668907, 2.346451, 0.653805, 1.432235,
2.510147, 2.241625, 2.940415, 1.862886, 1.046286, 1.304867, 2.633537,
2.628283, 3.495982, 0.755688, 1.1174, 1.370998, 0.570392, 6.777364
), Leptin = c(8.8071, 8.8438, 17.9393, 9.8827, 6.6994, 6.8317,
6.964, 4.311, 4.47, 17.127, 14.57, 35.59, 20.45, 8.88, 15.26,
14.09, 26.65, 30.7729, 21.2117, 49.3727, 15.1248, 28.7502, 45.6196,
39.2134, 12.331, 39.9802, 46.6401, 45.9624, 50.6094, 10.2809,
74.7069, 27.1841, 68.5102, 31.2128, 20.092, 21.9033, 16.2247,
83.4821, 24.2998, 21.7863, 28.562, 35.891, 10.39, 15.145, 56.502,
24.846, 65.926, 6.633, 45.272, 7.85, 21.778, 13.74, 7.6476, 7.7529,
11.2406, 16.7353, 19.0826, 9.6994, 11.0816, 9.8648, 8.438, 32.58,
6.3339, 8.0163, 14.9037, 17.9973, 8.6874, 12.1905, 12.87, 13.08,
7.65, 18.16, 9.62, 21.78, 10.16, 9.8, 37.2234, 25.7816, 47.647,
44.7059, 17.022, 38.8066, 46.076, 19.0653, 12.2617, 26.5166,
15.5325, 70.8824, 18.1314, 22.8884, 31.0385, 31.6453, 29.2739,
42.3914, 37.843, 51.3387, 12.6757, 14.3224, 53.4997, 14.7485,
31.1233, 14.9084, 44.0217, 26.8081, 33.1612, 41.4064, 23.8479,
18.69, 17.87, 50.53, 89.27, 54.68, 12.45, 61.48, 24.96, 90.28
), Adiponectin = c(9.7024, 5.429285, 22.43204, 7.16956, 4.81924,
13.67975, 5.589865, 13.25132, 10.35873, 11.57899, 13.11, 26.72,
23.67, 36.06, 17.95, 20.32, 38.04, 7.780255, 5.46262, 5.1, 8.6,
7.64276, 6.209635, 9.048185, 9.73138, 4.617125, 4.667645, 10.35526,
6.966895, 5.065915, 7.53955, 20.03, 4.7942, 8.300955, 3.19209,
2.19428, 4.267105, 6.796985, 2.19428, 8.12555, 7.36996, 9.34663,
9.000805, 11.78796, 8.13, 7.652055, 3.74122, 10.5673, 8.2863,
7.9317, 4.935635, 9.75326, 21.05663, 8.237405, 8.412175, 21.82375,
8.462915, 8.574655, 5.80762, 11.23624, 4.77192, 4.138025, 3.886145,
9.349775, 4.230105, 4.294705, 3.70523, 4.783985, 18.55, 20.37,
16.67, 16.1, 12.76, 17.86, 9.76, 6.420295, 11.01846, 12.71896,
5.357135, 13.49487, 16.44048, 10.63653, 21.57, 5.4861, 6.695585,
7.28287, 10.22231, 7.901685, 4.104105, 10.26266, 6.160995, 9.92365,
6.26854, 10.79394, 8.40443, 10.73174, 5.47817, 6.78387, 1.65602,
5.288025, 7.65222, 8.42996, 3.71009, 2.78491, 2.36495, 3.335665,
6.644245, 9.16, 11.9, 10.06, 8.01, 12.1, 21.42, 22.54, 33.75,
14.11), Resistin = c(7.99585, 4.06405, 9.27715, 12.766, 10.57635,
10.3176, 12.9361, 5.1042, 6.28445, 7.0913, 6.92, 4.58, 5.14,
6.85, 9.35, 7.64, 3.32, 13.68392, 6.70188, 17.10223, 9.1539,
5.62592, 24.6033, 16.43706, 10.19299, 8.70448, 11.78388, 23.3819,
22.03703, 15.72187, 22.32024, 10.26309, 21.44366, 6.71026, 10.37518,
4.2075, 3.29175, 82.1, 20.2535, 17.2615, 8.04375, 8.4156, 7.5767,
11.78796, 4.2989, 6.7052, 4.49685, 4.6638, 4.53, 9.6135, 8.49395,
11.774, 23.03408, 28.0323, 23.1177, 12.06534, 17.37615, 13.74244,
5.57055, 10.69548, 15.73606, 15.69876, 22.94254, 11.55492, 8.2049,
5.2633, 10.34455, 13.91245, 13.56, 4.62, 7.84, 5.31, 3.21, 4.82,
5.68, 16.1, 7.16514, 38.6531, 24.3701, 27.8325, 31.6904, 29.5583,
10.15726, 42.7447, 53.6717, 19.46324, 16.11032, 55.2153, 53.6308,
13.97399, 17.55503, 19.94687, 24.24591, 5.768, 11.50005, 20.76801,
23.03306, 26.0136, 49.24184, 16.48508, 18.35574, 14.91922, 20.4685,
14.76966, 9.9542, 6.89235, 15.55625, 8.89, 4.19, 11.73, 5.06,
10.96, 7.32, 10.33, 3.27, 4.35), MCP.1 = c(417.114, 468.786,
554.697, 928.22, 773.92, 530.41, 1256.083, 280.694, 136.855,
318.302, 354.6, 174.8, 313.73, 632.22, 165.02, 63.61, 191.72,
444.395, 252.449, 588.46, 534.224, 572.783, 904.981, 733.797,
1227.91, 586.173, 887.16, 1102.11, 667.928, 581.313, 864.968,
695.754, 358.624, 960.246, 473.859, 585.307, 634.602, 263.499,
378.996, 618.272, 698.789, 377.227, 335.393, 270.142, 200.976,
225.88, 206.802, 209.749, 215.769, 232.006, 45.843, 488.829,
552.444, 382.955, 573.63, 481.949, 321.919, 448.799, 90.6, 703.973,
199.055, 713.239, 737.672, 359.232, 355.31, 518.586, 635.049,
395.976, 301.21, 220.66, 193.87, 244.75, 513.66, 195.94, 312,
806.724, 483.377, 775.322, 1698.44, 783.796, 910.489, 426.175,
738.034, 799.898, 1041.843, 1698.44, 1698.44, 1078.359, 1698.44,
923.886, 638.261, 994.316, 764.667, 656.393, 396.021, 602.486,
407.206, 293.123, 256.001, 353.568, 572.401, 269.487, 396.648,
232.018, 655.834, 788.902, 621.273, 209.19, 198.4, 99.45, 218.28,
268.23, 330.16, 314.05, 392.46, 90.09)), class = "data.frame", row.names = c(NA,
-116L))
To get the z-scores you can use scale() and to get the covariance matrix you can use cov(). But, there is no reason to produce the covariance matrix from the z-scores instead of the raw data. You can also visualize a correlation matrix using the corrplot function in the corrplot package. The corrplot function will also take the raw data as an input. Not the covariance matrix.

Reformat daily stock and return data to weekly/monthly

I have some stock data together with some returns that are presented below.
Now I would like to coerce both the daily price changes (open, high, low, close, volume, adj. close) and the returns given to weekly or monthly values.
I know that the weekly prices can be obtained by xts::to.weekly(), but this drops the return. I don't know the exact mechanism behind the to.weekly function, but the returns need to be summarized with the sum function (I'm thinking of using xts::apply.weekly()), but then this would not be consistent with the stock price data....
How can this transition to weekly or monthly timescale be achieved efficiently?
Data <-
structure(c(64.5, 67, 72, 76, 75.75, 72, 75.5, 76, 76, 78, 78,
77.5, 79.25, 80, 76.25, 84, 89.75, 90.75, 92.25, 95.75, 94.5,
95, 92, 95.75, 100, 98, 104.25, 101.25, 100.25, 96.5, 94.75,
89, 94, 91.25, 99.25, 100.25, 100.25, 98, 96.5, 94.75, 97.5,
96.25, 99.25, 97, 98, 98.75, 97.25, 98.75, 100.25, 100.25, 103.25,
105.75, 108.5, 108.25, 103.75, 101.5, 99.75, 100, 99, 94.5, 99,
101.5, 105, 64.75, 73.25, 76.5, 76.75, 76, 75.75, 76, 76, 76,
80.75, 79, 79.5, 83, 80.5, 76.75, 91.5, 92.75, 94.75, 100.25,
96, 97.5, 96.75, 92, 100, 101.75, 104, 105, 103.25, 100.75, 99,
95.5, 92.75, 94, 97.75, 103.75, 101, 100.25, 99.5, 97.75, 96.75,
98, 99, 100, 100.75, 99.25, 98.75, 98, 102.25, 101, 103, 105,
109, 110.5, 108.25, 105, 102.25, 100.75, 100, 99.75, 98.75, 102.5,
103, 107.25, 60.75, 66.75, 71.25, 74.25, 72.25, 71.5, 74.25,
76, 76, 77, 75.75, 76, 79, 75.75, 73.25, 82, 88.75, 89.5, 91.75,
92.25, 92.75, 92, 92, 94, 97.75, 96.5, 98.5, 99.25, 93.5, 94,
88.25, 87.5, 91, 91, 98.75, 98.5, 100.25, 95.75, 95.5, 90.75,
96.75, 96, 95.25, 97, 95.75, 95.75, 96.75, 98.5, 97.25, 100.25,
102.5, 105.5, 107.5, 103.5, 102.75, 97.5, 98.25, 100, 94, 94.25,
98, 100, 103.75, 64.25, 72.75, 75.75, 75, 73.75, 75.5, 76, 76,
76, 79, 76, 79.25, 81.25, 76.75, 75.75, 88, 90.25, 93.75, 97.5,
95, 95, 92, 92, 100, 97.75, 102.75, 99.75, 100.25, 97.25, 94.5,
89, 91.75, 92.5, 97, 99.5, 100.25, 100.25, 96.75, 96, 96, 97.5,
98, 95.5, 100.75, 98, 96.75, 98, 100.25, 99.5, 102.25, 103.5,
108, 109.25, 104, 102.75, 99, 100, 100, 94.5, 97.5, 102, 103,
104.5, 6808900, 8180500, 5628500, 3238900, 3765800, 3177100,
887600, 0, 0, 3923200, 2425700, 3331200, 4058600, 3682800, 3293500,
10525000, 5664200, 3982600, 4702300, 6479800, 2565300, 2480500,
0, 3653000, 3400, 4010500, 5145800, 2782200, 3925100, 2770700,
4618500, 2712300, 1675400, 3331500, 5343000, 1169700, 0, 2095400,
1016600, 3642600, 3729400, 3575300, 3396900, 1963400, 3547300,
1865100, 2496300, 3226800, 2333200, 4285900, 3933000, 7400700,
3325200, 1848400, 21000, 4273700, 1910400, 0, 3168000, 2356000,
2184800, 1950300, 2649900, 51.5865, 58.4112, 60.8199, 60.2178,
59.2141, 60.6192, 61.0207, 61.0207, 61.0207, 63.4294, 61.0207,
63.6301, 65.2359, 61.6228, 60.8199, 70.6555, 72.462, 75.2722,
78.2831, 76.2758, 76.2758, 73.8671, 73.8671, 80.2903, 78.4838,
82.4983, 80.0896, 80.4911, 78.0824, 75.8744, 71.4584, 73.6664,
74.2686, 77.8816, 79.8889, 80.4911, 80.4911, 77.6809, 77.0787,
77.0787, 78.2831, 78.6845, 76.6773, 80.8925, 78.6845, 77.6809,
78.6845, 80.4911, 79.8889, 82.0969, 83.1005, 86.7136, 87.7172,
83.5019, 82.4983, 79.4874, 80.2903, 80.2903, 75.8744, 78.2831,
81.8961, 82.699, 83.9034, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.086956169, -0.022499605, 0.051150683,
-0.029196965, 0.005013135, -0.029925048, -0.028277819, -0.058201449,
0.030899097, 0.00817469, 0.048647746, 0.025773739, 0.007537968,
0, -0.034913177, -0.007752227, 0, 0.015625588, 0.005127543, -0.025509471,
0.054973245, -0.027295485, -0.012754736, 0.012919521, 0.022960049,
-0.007481572, 0.027638383, 0.012224579, 0.043478679, 0.011573732,
-0.048055569, -0.012018888, -0.03649651, 0.010100972, 0, 0, 0,
0.046153001, 0.009803886, 0.014563659), .Dim = c(63L, 7L), .Dimnames = list(
NULL, c("Open", "High", "Low", "Close", "Volume", "Adj.Close",
"Return")), index = structure(c(1238544000, 1238630400, 1238716800,
1238976000, 1239062400, 1239148800, 1239235200, 1239321600, 1239580800,
1239667200, 1239753600, 1239840000, 1239926400, 1240185600, 1240272000,
1240358400, 1240444800, 1240531200, 1240790400, 1240876800, 1240963200,
1241049600, 1241136000, 1241395200, 1241481600, 1241568000, 1241654400,
1241740800, 1.242e+09, 1242086400, 1242172800, 1242259200, 1242345600,
1242604800, 1242691200, 1242777600, 1242864000, 1242950400, 1243209600,
1243296000, 1243382400, 1243468800, 1243555200, 1243814400, 1243900800,
1243987200, 1244073600, 1244160000, 1244419200, 1244505600, 1244592000,
1244678400, 1244764800, 1245024000, 1245110400, 1245196800, 1245283200,
1245369600, 1245628800, 1245715200, 1245801600, 1245888000, 1245974400),
tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date",
tclass = "Date", .indexTZ = "UTC", tzone = "UTC")
Use period.apply (or apply.daily, apply.weekly, etc.) with your own custom function. Something like:
library(quantmod) # for Op, Hi, Lo, Cl, and Vo functions
myFun <- function(x) {
# need coredata, so c.xts will not be dispatched
cx <- coredata(x)
c(Open = first(Op(cx)),
Hi = max(Hi(cx)),
Low = min(Lo(cx)),
Close = last(cx[,"Close"]),
Volume = sum(Vo(cx)),
Adj.Close = last(cx[,"Adj.Close"]),
Return = sum(cx[,"Return"]))
}
out <- period.apply(Data, endpoints(Data, "months"), myFun)
# Open Hi Low Close Volume Adj.Close Return
# 2009-04-30 64.5 100.25 60.75 92.0 88802000 73.8671 0.0000000
# 2009-05-29 92.0 105.00 87.50 95.5 58597300 76.6773 0.0486306
# 2009-06-26 97.0 110.50 94.00 104.5 54739400 83.9034 0.1222869

ggvis barchart using dates as x axis

I have been switching over from ggplot to ggvis when working with shiny apps. I have figured out a lot but am really stumped when it comes to bar graphs. I have a timeseries with dates and values. I simply want bars instead of points for each value (I would ideally like to be able to plot multiple semi-transparent bars if anyone has had success there please share) but here I wanted to get one bar working.
Works with layer_points()
df %>% ggvis(~date, ~x) %>% layer_points() %>% scale_datetime("x")
Doesnt work with layer_bars()
df %>% ggvis(~date, ~x) %>% layer_bars() %>% scale_datetime("x")
Data I am using...
structure(list(date = structure(c(7680, 7687, 7694, 7701, 7708,
7715, 7722, 7729, 7736, 7743, 7750, 7757, 7764, 7771, 7778, 7785,
7792, 7799, 7806, 7813, 7820, 7827, 7834, 7841, 7848, 7855, 7862,
7869, 7876, 7883, 7890, 7897, 7904, 7911, 7918, 7925, 7932, 7939,
7946, 7953, 7960, 7967, 7974, 7981, 7988, 7995, 8002, 8009, 8016,
8023, 8030, 8037, 8044, 8051, 8058, 8065, 8072, 8079, 8086, 8093,
8100, 8107, 8114, 8121, 8128, 8135, 8142, 8149, 8156, 8163, 8170,
8177, 8184, 8191, 8198, 8205, 8212, 8219, 8226, 8233, 8240, 8247,
8254, 8261, 8268, 8275, 8282, 8289, 8296, 8303, 8310, 8317, 8324,
8331, 8338, 8345, 8352, 8359, 8366, 8373, 8380, 8387, 8394, 8401,
8408, 8415, 8422, 8429, 8436, 8443, 8450, 8457, 8464, 8471, 8478,
8485, 8492, 8499, 8506, 8513, 8520, 8527, 8534, 8541, 8548, 8555,
8562, 8569, 8576, 8583, 8590, 8597, 8604, 8611, 8618, 8625, 8632,
8639, 8646, 8653, 8660, 8667, 8674, 8681, 8688, 8695, 8702, 8709,
8716, 8723, 8730, 8737, 8744, 8751, 8758, 8765, 8772, 8779, 8786,
8793, 8800, 8807, 8814, 8821, 8828, 8835, 8842, 8849, 8856, 8863,
8870, 8877, 8884, 8891, 8898, 8905, 8912, 8919, 8926, 8933, 8940,
8947, 8954, 8961, 8968, 8975, 8982, 8989, 8996, 9003, 9010, 9017,
9024, 9031, 9038, 9045, 9052, 9059, 9066, 9073), class = "Date"),
x = c(-0.034038302, 0.122310949, -0.002797319, 0.026515253,
0.039961798, 0.034473263, 0.00549937, -0.024125944, 0.000132490000000001,
0.011038357, -0.02135072, 0.030663311, -0.008915551, 0.004855042,
0.01563688, -0.007397493, 0.013569146, -0.004968811, -0.00250391,
0.014624532, 0.036937453, -0.023685917, 0.018921356, -0.003066779,
-0.009217771, 0.005317513, 0.010378968, 0.001580798, -0.015085972,
-0.000121644000000001, 0.020468644, 0.007925229, 0.007721276,
-0.003123545, -0.018317891, -0.014900591, 0.003260844, -0.001565358,
-0.014833886, 0.00366766, 0.014297139, -0.00725552, 0.012207931,
0.024035152, -0.024195095, -0.0043564, 0.000847468, 0.033031596,
0.023685033, 0.025143071, 0.046264348, 0.038285177, -0.009180356,
-0.01630399, -0.010131294, -0.009939386, -0.007620427, 0.013062259,
0.009912238, 0.000192973, -0.01683559, -0.002627549, 0.019836063,
-0.019946159, -0.020124331, 0.012921737, 0.034604405, -0.020774015,
0.00334805, 0.002271156, -0.018676732, 0.019160923, -0.01945997,
-0.014342636, -0.004867796, -0.010002446, -0.004372991, 0.023164369,
0.019824112, -0.00321832, -0.015785746, 0.040836652, 0.00148831,
0.012084485, -0.009603897, -0.004642148, -0.008399234, 0.010463218,
0.000256571000000001, -0.01978405, -0.003439498, -0.015669975,
0.026180724, 0.020373255, 0.019160773, 0.00692683, 0.010215506,
0.010861939, 0.012041143, 0.025734568, -0.004828156, 0.006914552,
-0.00720089, -0.000538489999999999, -0.008479448, 0.022926604,
0.002131842, -0.003688597, 0.025325639, -0.009562293, -0.024336741,
0.012907537, 0.004339383, 0.010744364, -0.013058765, -0.003672014,
-0.023887493, 0.01062259, 0.02088054, -0.035249878, -0.001462821,
0.01904368, -0.001308787, 0.009203217, 0.019856479, 0.011296979,
0.010039545, -0.01559142, 0.006083419, -0.017958978, -0.007488063,
0.01236649, -0.004459064, -0.004375386, 0.025500722, 0.005557851,
0.008444321, 0.002827649, 0.020320308, 0.031611803, -0.010199803,
-0.009425874, 0.007942729, -2.59379999999999e-05, 0.016669077,
-0.011666062, 0.022835386, -0.025599107, 0.013562535, -0.018365192,
0.018148786, 0.016649144, -0.009530455, 0.012996597, 0.002034778,
-0.005926478, -0.004897238, -0.004419719, 0.010848926, -0.006039757,
-0.030287605, 0.019221837, 0.001808161, -0.009566133, 0.005009292,
0.005365023, -0.004879922, -0.024637933, -0.0186584, 0.004786059,
-0.008245254, -0.000106243, -0.001714888, -0.017804006, -0.021200061,
0.003812757, 0.021940886, 0.002270448, -0.015417493, -0.045754612,
-0.003468442, -0.006242659, 0.022383824, -0.018753927, 0.008577571,
0.008655048, 0.02374636, 0.029522811, 0.009946946, 0.015419714,
-0.016714623, -0.014616188, 0.019670855, -0.038979063, 0.020491563,
-0.009640674, 0.046051144, -0.021434575, 0.000190443999999998,
-0.029013969), id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136,
137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148,
149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
197, 198, 199, 200)), .Names = c("date", "x", "id"), row.names = 53:252, class = "data.frame")
Set format df$date as character:
df$date <- as.character(df$date)
and then:
df %>% ggvis(~date, ~x) %>% layer_bars()

Resources