How to find and plot correlated variables in R? - 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.

Related

ggbetweenstats - point size by variable

Is there a way to change the point size by variable in ggbetweenstats?
So far, I could get this:
SNV <- c(82, 90, 93, 83, 86, 86, 70, 81, 104, 90, 86, 98, 96, 91, 87, 92, 86, 99, 79, 94, 94, 84, 99, 77, 62, 70, 66, 60, 64, 70, 75, 54, 59, 66, 59, 52, 61, 74, 67, 90, 99, 97, 85, 96, 96, 74, 89, 107, 96, 94, 104, 106, 100, 96, 100, 90, 102, 81, 104, 96, 90, 101, 78, 74, 73, 71, 67, 67, 72, 76, 56, 62, 72, 63, 62, 65, 77, 72)
Log.CPI <- c(13.52, 0.00, 16.15, 12.35, 0.00, 0.00, 4.23, 5.14, 0.00, 13.45, 9.67, 9.21, 11.38, 9.64, 18.40, 15.21, 9.88, 13.94, 0.00, 14.54, 0.00, 11.96, 15.22, 6.74, 3.69, 9.86, 4.70, 9.23, 4.57, 10.83, 6.56, 3.97, 10.16, 9.03, 4.47, 5.06, 7.47, 5.61, 4.17, 13.87, 0.00, 16.36, 11.90, 0.00, 0.00, 9.19, 9.39, 0.00, 13.55, 15.54, 10.59, 14.77, 11.39, 18.41, 15.55, 10.71, 13.96, 0.00, 14.93, 0.00, 11.98, 15.89, 9.11, 7.07, 9.88, 7.87, 9.03, 6.40, 11.10, 7.82, 5.71, 9.78, 9.77, 6.43, 5.46, 7.51, 7.82, 8.46)
Method <- rep(c("CPI_trad","CPI_new"),each=39)
df <- data.frame(SNV, Log.CPI, Method)
library(ggstatsplot)
library(tidyverse)
df$Method <- factor(df$Method, levels=c("CPI_trad", "CPI_new"))
ggbetweenstats(
plot.type = "box",
p.adjust.method = "none",
data = df,
x = Method,
y = SNV,
point.args = list(position = ggplot2::position_jitterdodge(dodge.width = 0.6), alpha
= 0.4, size = 4, stroke = 0))
I want points to inherit size from the variable "Log.CPI". Something like this in ggplot:
ggplot() +
geom_boxplot(data=df, aes(x=Method, y=SNV),
outlier.shape = NA, width=0.5) +
geom_point(data = df,
aes(x=Method, y=SNV, color = Method, size = Log.CPI),
position=position_jitter(h=0,w=0.15), alpha = 0.5) +
scale_size(breaks = c(0,4,8,12,16,20), limits = c(0,20)) +
guides(colour = guide_legend(override.aes = list(size=5))) +
theme_classic()
changing point.args = list(size = Log.CPI) in ggbetweenstats doesn’t do the trick:
point.args = list(position = ggplot2::position_jitterdodge(dodge.width = 0.6), alpha
= 0.4, size = Log.CPI, stroke = 0))

Within a simple linear regression in R, how do I rescale age to estimate it's beta-coefficient per per year/5 years/10 years?

This might be a bit of a dumb question, but roaming around SO and other websites I can't find a straightforward answer: I've got data on the relationship between age and a continuous outcome:
library(dplyr)
library(tidyverse)
library(magrittr)
mydata <-
structure(list(ID = c(104, 157, 52, 152, 114, 221, 320, 125,
75, 171, 80, 76, 258, 82, 142, 203, 37, 92, 202, 58, 194, 38,
4, 137, 25, 87, 40, 117, 21, 255, 277, 315, 96, 134, 185, 94,
3, 153, 172, 65, 279, 209, 60, 13, 154, 160, 24, 29, 159, 213,
127, 74, 48, 126, 184, 132, 61, 141, 27, 49, 8, 39, 164, 162,
34, 205, 179, 119, 77, 135, 138, 165, 103, 253, 14, 20, 310,
84, 30, 273, 22, 105, 262, 116, 86, 83, 145, 31, 95, 51, 81,
271, 36, 50, 189, 2, 115, 7, 197, 54), age = c(67.1, 70.7, 53,
61.7, 66.1, 57.7, 54.1, 67.2, 60.9, 55.8, 40.7, 57.6, 64.1, 70.7,
47.5, 46.3, 66.7, 55, 63.3, 68.2, 61.2, 60.5, 52, 65.3, 48.9,
56.9, 62.7, 75.2, 61.4, 57.9, 53.6, 58.1, 51, 67.3, 63.9, 57,
43.2, 64.7, 62.8, 56.3, 51.7, 39.4, 45.2, 57.8, 55.7, 69.6, 61.5,
50.1, 73.7, 55.5, 65.2, 54.6, 49, 35.2, 52.9, 46.3, 55, 52.5,
54.2, 61, 57.4, 56.5, 53.6, 47.7, 64.2, 53.4, 60.9, 58.2, 60.7,
50.3, 48.3, 74.7, 52.1, 59.9, 52.4, 70.8, 61.2, 66.5, 55.4, 57.5,
59.2, 60.1, 52.3, 60.2, 54.8, 36.3, 61.5, 48.6, 56, 62, 64.8,
40.4, 68.3, 60, 69.1, 56.6, 45.3, 58.5, 52.3, 52), continuous_outcome = c(3636.6,
1128.2, 2007.5, 802.9, 332.3, 2636.1, 169.5, 67.9, 3261.8, 1920.3,
155.2, 1677.2, 198.2, 11189.7, 560.9, 633.1, 196.1, 13.9, 100.7,
7594.5, 1039.8, 83.9, 2646.8, 284.6, 306, 1135.6, 1883.1, 5681.4,
1706.2, 2241.1, 97.7, 1106.8, 1107.1, 290.8, 2123.4, 267, 115.3,
138.5, 152.7, 1338.9, 6709.8, 561.7, 1931.7, 3112.4, 1876.3,
3795.9, 5706.7, 7.4, 1324.9, 4095.4, 205.4, 1886, 177.3, 304.4,
1319.1, 415.9, 537.2, 3141.1, 740, 1976.7, 624.8, 983.1, 1163.5,
1432.6, 3730.4, 2023.4, 498.2, 652.5, 982.7, 1345.3, 138.4, 1505.1,
3528.1, 11.9, 884.5, 10661.6, 1911.4, 2800.8, 81.5, 396.4, 409.1,
417.3, 186, 1892.4, 1689.7, 0, 210.1, 210.5, 3484.5, 3196.8,
57.2, 20.2, 947, 540, 1603.1, 1571.8, 9.1, 149.2, 122, 63.2)), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))
As you can see in the tibble, age is a continuous variable measured to precision of 1 decimal place:
head(mydata)
# A tibble: 6 x 3
ID age continuous_outcome
<dbl> <dbl> <dbl>
1 104 67.1 3637.
2 157 70.7 1128.
3 52 53 2008.
4 152 61.7 803.
5 114 66.1 332.
6 221 57.7 2636.
When I fit a simple linear regression (for now assuming all assumptions are not-violated) I get the following beta-coefficient:
fit <-
lm(formula=continuous_outcome ~ age,
data=mydata)
fit
Call:
lm(formula = continuous_outcome ~ age, data = mydata)
Coefficients:
(Intercept) age
-3400.12 86.06
The beta-coefficient for age is 86.06. Does this mean that, as age is measured to 1 decimal place, that for every 0.1 years increase my outcome increases by 86.06? If so, how do I rescale age so that I am measuring the effect of age per, for example, 5 years or 10 years?
Thanks in advance!
The beta coefficient shows the amount that the dependent variable (DV, in this case continuous_outcome) will increase for every one unit increase in your independent variable (IV, in this case age in years).
If you want to show the relationship per 1/10th of a year, multiply your age column before fitting the model, or divide the beta coefficient by 10.
For your specific requests, since the beta coefficient is 86.06, you can multiply this by the number of years to get the increase of the continuous variable. So:
1 year increase = +86.06
5 year increase = +430.3 (86.06 * 5)
10 year increase = +860.6 (86.06 * 10)
To answer the last question (The estimate for the effect of age per 5 years), that would be 430.3, which is 86.06 * 5. So for every 5 years that a persons age increases, the continuous_outcome increases by 430.3 on average.

How to change the color of points in a specific quadrant?

I have a scatter plot of data points, and I am able to use abline to create the quadrant lines. Now I'm trying to color the points of those in quadrant 1 and quadrant 3. I am going to plot all the points first, and then plot only those in quadrant one in red, and those in quadrant three in blue. They will be superimposed on the old ones. However, I don't know how to plot points by quadrant. Thank you.
"write the R code to plot the points that are in the(Q1,Q1) quadrant of(waiting, eruption) in Red, AND the points that are in the(Q3,Q3) Quadrant of(waiting, eruption) in blue, everything else in black, use a pch=19 and cex=0.5. (Hint, plot everything with pch=19, col="black" first, then add the conditional points with the required colors, they will be superimposed on the old ones, whatever left stays in black)."
This is my foundation:
plot(faithful$waiting, faithful$eruptions, pch=19, cex=0.5, col="black")
I realize I somehow need to plot only those in quadrant 1 in a color and then plot those in quadrant 3 in another color, and run that code after my foundation. But I'm not sure how. Thanks.
The dput(faithful) is:
structure(list(eruptions = c(3.6, 1.8, 3.333, 2.283, 4.533, 2.883,
4.7, 3.6, 1.95, 4.35, 1.833, 3.917, 4.2, 1.75, 4.7, 2.167, 1.75,
4.8, 1.6, 4.25, 1.8, 1.75, 3.45, 3.067, 4.533, 3.6, 1.967, 4.083,
3.85, 4.433, 4.3, 4.467, 3.367, 4.033, 3.833, 2.017, 1.867, 4.833,
1.833, 4.783, 4.35, 1.883, 4.567, 1.75, 4.533, 3.317, 3.833,
2.1, 4.633, 2, 4.8, 4.716, 1.833, 4.833, 1.733, 4.883, 3.717,
1.667, 4.567, 4.317, 2.233, 4.5, 1.75, 4.8, 1.817, 4.4, 4.167,
4.7, 2.067, 4.7, 4.033, 1.967, 4.5, 4, 1.983, 5.067, 2.017, 4.567,
3.883, 3.6, 4.133, 4.333, 4.1, 2.633, 4.067, 4.933, 3.95, 4.517,
2.167, 4, 2.2, 4.333, 1.867, 4.817, 1.833, 4.3, 4.667, 3.75,
1.867, 4.9, 2.483, 4.367, 2.1, 4.5, 4.05, 1.867, 4.7, 1.783,
4.85, 3.683, 4.733, 2.3, 4.9, 4.417, 1.7, 4.633, 2.317, 4.6,
1.817, 4.417, 2.617, 4.067, 4.25, 1.967, 4.6, 3.767, 1.917, 4.5,
2.267, 4.65, 1.867, 4.167, 2.8, 4.333, 1.833, 4.383, 1.883, 4.933,
2.033, 3.733, 4.233, 2.233, 4.533, 4.817, 4.333, 1.983, 4.633,
2.017, 5.1, 1.8, 5.033, 4, 2.4, 4.6, 3.567, 4, 4.5, 4.083, 1.8,
3.967, 2.2, 4.15, 2, 3.833, 3.5, 4.583, 2.367, 5, 1.933, 4.617,
1.917, 2.083, 4.583, 3.333, 4.167, 4.333, 4.5, 2.417, 4, 4.167,
1.883, 4.583, 4.25, 3.767, 2.033, 4.433, 4.083, 1.833, 4.417,
2.183, 4.8, 1.833, 4.8, 4.1, 3.966, 4.233, 3.5, 4.366, 2.25,
4.667, 2.1, 4.35, 4.133, 1.867, 4.6, 1.783, 4.367, 3.85, 1.933,
4.5, 2.383, 4.7, 1.867, 3.833, 3.417, 4.233, 2.4, 4.8, 2, 4.15,
1.867, 4.267, 1.75, 4.483, 4, 4.117, 4.083, 4.267, 3.917, 4.55,
4.083, 2.417, 4.183, 2.217, 4.45, 1.883, 1.85, 4.283, 3.95, 2.333,
4.15, 2.35, 4.933, 2.9, 4.583, 3.833, 2.083, 4.367, 2.133, 4.35,
2.2, 4.45, 3.567, 4.5, 4.15, 3.817, 3.917, 4.45, 2, 4.283, 4.767,
4.533, 1.85, 4.25, 1.983, 2.25, 4.75, 4.117, 2.15, 4.417, 1.817,
4.467), waiting = c(79, 54, 74, 62, 85, 55, 88, 85, 51, 85, 54,
84, 78, 47, 83, 52, 62, 84, 52, 79, 51, 47, 78, 69, 74, 83, 55,
76, 78, 79, 73, 77, 66, 80, 74, 52, 48, 80, 59, 90, 80, 58, 84,
58, 73, 83, 64, 53, 82, 59, 75, 90, 54, 80, 54, 83, 71, 64, 77,
81, 59, 84, 48, 82, 60, 92, 78, 78, 65, 73, 82, 56, 79, 71, 62,
76, 60, 78, 76, 83, 75, 82, 70, 65, 73, 88, 76, 80, 48, 86, 60,
90, 50, 78, 63, 72, 84, 75, 51, 82, 62, 88, 49, 83, 81, 47, 84,
52, 86, 81, 75, 59, 89, 79, 59, 81, 50, 85, 59, 87, 53, 69, 77,
56, 88, 81, 45, 82, 55, 90, 45, 83, 56, 89, 46, 82, 51, 86, 53,
79, 81, 60, 82, 77, 76, 59, 80, 49, 96, 53, 77, 77, 65, 81, 71,
70, 81, 93, 53, 89, 45, 86, 58, 78, 66, 76, 63, 88, 52, 93, 49,
57, 77, 68, 81, 81, 73, 50, 85, 74, 55, 77, 83, 83, 51, 78, 84,
46, 83, 55, 81, 57, 76, 84, 77, 81, 87, 77, 51, 78, 60, 82, 91,
53, 78, 46, 77, 84, 49, 83, 71, 80, 49, 75, 64, 76, 53, 94, 55,
76, 50, 82, 54, 75, 78, 79, 78, 78, 70, 79, 70, 54, 86, 50, 90,
54, 54, 77, 79, 64, 75, 47, 86, 63, 85, 82, 57, 82, 67, 74, 54,
83, 73, 73, 88, 80, 71, 83, 56, 79, 78, 84, 58, 83, 43, 60, 75,
81, 46, 90, 46, 74)), row.names = c(NA, 272L), class = "data.frame")
And how I defined my quadrants:
plot(faithful$waiting, faithful$eruptions, pch=19, cex=0.5, col="green")
abline(v=quantile(faithful$waiting, 0.75), col="red")
abline(h=quantile(faithful$eruptions,0.75),col="red")
abline(v=quantile(faithful$waiting, 0.25), col="red")
abline(h=quantile(faithful$eruptions,0.25),col="red")
mtext("Q1", side=1, at=quantile(faithful$waiting, 0.25), col="red")
mtext("Q1", side=2, at=quantile(faithful$eruptions, 0.25), col="red")
mtext("Q3", side=2, at=quantile(faithful$eruptions, 0.75), col="red")
mtext("Q3", side=1, at=quantile(faithful$waiting, 0.75), col="red")
What I've been trying, when using a grouping:
faithful$color<- 'black'
q1<-quantile(faithful$waiting, 0.25) #how do I assign the quantiles
q3<-quantile(faithful$waiting, 0.75)
faithful$color[faithful$waiting ]="red" #how do I then assign the quantiles a color
faithful$color[faithful$waiting ]="blue"
We can cut both variables into quantiles and paste the levels together to obtain a group for each quadrant.
faithful <- within(faithful, {
e.qtl <- as.integer(cut(faithful$eruptions,
quantile(faithful$eruptions, c(0, .25, .75, 1)),
include.lowest=TRUE))
w.qtl <- as.integer(cut(faithful$waiting,
quantile(faithful$waiting, c(0, .25, .75, 1)),
include.lowest=TRUE))
qtl <- as.integer(as.factor(with(faithful,
paste0(e.qtl, w.qtl))))
})
Then we can easily plot the data assigning color for each group.
colors <- rainbow(length(unique(faithful$qtl))) # create colors
# plot
plot(faithful$waiting, faithful$eruptions, type="n") # empty plot here
with(faithful, points(waiting, eruptions, col=colors[qtl], pch=19, cex=0.5))
After that add your ablines and mtext.
abline(v=quantile(faithful$waiting, 0.75), col="red")
abline(h=quantile(faithful$eruptions, 0.75),col="red")
abline(v=quantile(faithful$waiting, 0.25), col="red")
abline(h=quantile(faithful$eruptions, 0.25),col="red")
mtext("Q1", side=1, at=quantile(faithful$waiting, 0.25), col="red")
mtext("Q1", side=2, at=quantile(faithful$eruptions, 0.25), col="red")
mtext("Q3", side=2, at=quantile(faithful$eruptions, 0.75), col="red")
mtext("Q3", side=1, at=quantile(faithful$waiting, 0.75), col="red")
Result

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

Selecting just some rows of a RGList

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.

Resources