R: How to change atomic vector to recursive? - r

I have been trying to use the $ function to extract a certain column from a list of data frames, and always get the same error.
For reference, here is the code I used to read the data and summarize it into a list:
data_files <- list.files("D:\\My\\Data\\Path\\")
mctd_list<-list() # create empty list
for (i in 1:length(data_files)){
list[[i]]<-as.data.frame(read.csv2(data_files[i]))
} # fill in data into list
So far, so good, no issues here. But when I try to delete rows with a negative value in a certain column from each data frame, I get the error. Here's the loop for removing my rows:
for(i in 1:length(list)){
list<-list[[i]][list[[i]]$column_x>=0,]
}
Now this doesn't work, and I get the error:
$ operator is invalid for atomic vectors
To check, if the list really was atomic, I checked the following:
is.atomic(list[[i]]) # Output: TRUE
is.recursive(list[[i]]) # Output: FALSE
Apparently the data is really atomic, but I can not find a way to transform it into a recursive. I tried
as.data.frame(list[[i]])
before the loop, but it seems to be doing nothing. Afterwards, the list is still atomic.
Is there something I'm missing, or an easy way to get around the issue? I would like to avoid using [[]] for extraction, because this brings with it a bunch of new problems.
P.s. here the dput() Output:
data <- as.data.frame(t(list[[i]])) # to transform into recursive
> dput(data)
structure(list(V1 = c(44854.79028, 19.19, -0.27, 0.01, 0, 1479.2
), V2 = c(44854.79063, 19.01, -0.27, 0.01, 0, 1478.64), V3 = c(44854.79097,
18.72, -0.21, 0.01, 0, 1477.69), V4 = c(44854.79132, 18.45, -0.27,
0.01, 0, 1476.82), V5 = c(44854.79167, 18.06, -0.32, 0.01, 0,
1475.56), V6 = c(44854.79201, 17.64, -0.32, 0.01, 0, 1474.19),
V7 = c(44854.79236, 8.41, -1.35, 17.05, 19.03, 1461.43),
V8 = c(44854.79271, 4.69, 4.48, 21.48, 21.29, 1451.97), V9 = c(44854.79306,
2.95, 9.21, 25.62, 23.82, 1449.9), V10 = c(44854.7934, 2.22,
13.08, 26.96, 24.44, 1448.52), V11 = c(44854.79375, 2.07,
16.76, 27.83, 25.05, 1449.04), V12 = c(44854.7941, 1.83,
21.3, 28.6, 25.51, 1449.09), V13 = c(44854.79444, 1.91, 27.65,
29.26, 26.1, 1450.41), V14 = c(44854.79479, 2.14, 32.79,
29.68, 26.63, 1452.08), V15 = c(44854.79514, 2.37, 37.56,
29.89, 26.98, 1453.46), V16 = c(44854.79549, 2.41, 43.58,
30.16, 27.23, 1454.07), V17 = c(44854.79583, 2.41, 50.66,
30.39, 27.42, 1454.49), V18 = c(44854.79618, 2.37, 58.82,
30.63, 27.59, 1454.77), V19 = c(44854.79653, 2.26, 67.14,
30.8, 27.65, 1454.63), V20 = c(44854.79688, 2.18, 75.72,
31.01, 27.76, 1454.71), V21 = c(44854.79722, 2.03, 84.84,
31.42, 27.97, 1454.71), V22 = c(44854.79757, 2.18, 93.68,
31.74, 28.35, 1455.96), V23 = c(44854.79792, 2.26, 101.75,
31.8, 28.47, 1456.51), V24 = c(44854.79826, 2.3, 102.45,
31.76, 28.47, 1456.64), V25 = c(44854.79861, 2.3, 101.04,
31.76, 28.47, 1456.62), V26 = c(44854.79896, 2.3, 98.83,
31.74, 28.45, 1456.55), V27 = c(44854.79931, 2.3, 90.89,
31.51, 28.26, 1456.12), V28 = c(44854.79965, 2.11, 83.34,
31.34, 27.97, 1454.93), V29 = c(44854.8, 1.99, 75.46, 31.17,
27.74, 1454.06), V30 = c(44854.80035, 1.99, 66.89, 30.99,
27.58, 1453.68), V31 = c(44854.80069, 2.18, 58.35, 30.97,
27.72, 1454.38), V32 = c(44854.80104, 2.3, 50.29, 30.75,
27.63, 1454.46), V33 = c(44854.80139, 2.37, 43.26, 30.57,
27.53, 1454.44), V34 = c(44854.80174, 2.53, 36.86, 30.33,
27.46, 1454.7), V35 = c(44854.80208, 2.37, 30.09, 29.92,
27, 1453.37), V36 = c(44854.80243, 2.03, 22.85, 29.46, 26.36,
1451.12), V37 = c(44854.80278, 1.79, 16.08, 29.01, 25.81,
1449.38), V38 = c(44854.80313, 1.44, 10.6, 28.54, 25.17,
1447.09), V39 = c(44854.80347, 1.25, 5.7, 26.58, 23.46, 1443.52
), V40 = c(44854.80382, 0.93, 1.5, 24.88, 21.87, 1439.74),
V41 = c(44854.80417, 0.33, -1.03, 0, 0, 1403.66), V42 = c(44854.80451,
0.37, -0.97, 0, 0, 1403.86), V43 = c(44854.80486, 0.41, -1.03,
0, 0, 1404.06), V44 = c(44854.80521, 0.37, -0.97, 0, 0, 1403.86
), V45 = c(44854.80556, 0.37, -1.03, 0, 0, 1403.86), V46 = c(44854.8059,
0.29, -0.97, 0, 0, 1403.47), V47 = c(44854.80625, 0.25, -0.97,
0, 0, 1403.27), V48 = c(44854.8066, 0.17, -0.97, 0, 0, 1402.87
), V49 = c(44854.80694, 0, -1.51, 4.89, 4.7, 1408.6), V50 = c(44854.80729,
-0.08, -1.51, 4.83, 4.64, 1408.13), V51 = c(44854.80764,
-0.12, -1.46, 4.81, 4.62, 1407.91), V52 = c(44854.80799,
-0.16, -1.46, 4.8, 4.6, 1407.69), V53 = c(44854.80833, -0.24,
-1.46, 4.77, 4.56, 1407.24), V54 = c(44854.80868, -0.28,
-1.41, 4.77, 4.56, 1407.05), V55 = c(44854.80903, -0.36,
-1.41, 4.74, 4.52, 1406.6), V56 = c(44854.80938, -0.4, -1.41,
4.72, 4.5, 1406.37), V57 = c(44854.80972, -0.4, -1.41, 4.72,
4.5, 1406.37), V58 = c(44854.81007, -0.45, -1.41, 4.23, 4.05,
1405.51), V59 = c(44854.81042, -0.45, -1.46, 4.21, 4.03,
1405.48), V60 = c(44854.81076, -0.45, -1.41, 4.21, 4.03,
1405.48), V61 = c(44854.81111, -0.45, -1.46, 4.19, 4.01,
1405.45), V62 = c(44854.81146, -0.49, -1.41, 4.19, 4.01,
1405.26), V63 = c(44854.81181, -0.49, -1.41, 4.19, 4.01,
1405.26), V64 = c(44854.81215, -0.49, -1.41, 4.17, 3.99,
1405.23), V65 = c(44854.8125, -0.49, -1.41, 4.17, 3.99, 1405.23
), V66 = c(44854.81285, -0.49, -1.41, 4.17, 3.99, 1405.23
), V67 = c(44854.81319, -0.49, -1.41, 4.17, 3.99, 1405.23
), V68 = c(44854.81354, -0.53, -1.41, 4.18, 3.99, 1405.03
), V69 = c(44854.81389, -0.53, -1.46, 4.18, 3.99, 1405.03
), V70 = c(44854.81424, -0.57, -1.41, 4.16, 3.97, 1404.8),
V71 = c(44854.81458, -0.61, -1.35, 4.16, 3.97, 1404.6), V72 = c(44854.81493,
-0.61, -1.46, 4.16, 3.97, 1404.6), V73 = c(44854.81528, -0.61,
-1.41, 4.14, 3.95, 1404.57), V74 = c(44854.81563, -0.65,
-1.35, 5.12, 4.82, 1405.69), V75 = c(44854.81597, -0.73,
-1.35, 4.86, 4.57, 1404.92), V76 = c(44854.81632, -0.73,
-1.35, 3.91, 3.73, 1403.64), V77 = c(44854.81667, -0.69,
-1.35, 3.53, 3.39, 1403.33), V78 = c(44854.81701, -0.65,
-1.3, 3.57, 3.43, 1403.59), V79 = c(44854.81736, -0.61, -1.35,
0.33, 0.35, 1399.44), V80 = c(44854.81771, -0.53, -1.35,
0.76, 0.79, 1400.43), V81 = c(44854.81806, -0.4, -1.19, 3.81,
3.67, 1405.15), V82 = c(44854.8184, -0.32, -1.08, 3.73, 3.61,
1405.45), V83 = c(44854.81875, -0.24, -1.03, 3.92, 3.79,
1406.11), V84 = c(44854.8191, -0.12, -1.14, 0.24, 0.27, 1401.79
), V85 = c(44854.81944, -0.04, -0.92, 3.29, 3.23, 1406.27
), V86 = c(44854.81979, 0.05, -0.92, 0.39, 0.43, 1402.8),
V87 = c(44854.82014, 0.13, -0.86, 0.28, 0.31, 1403.04), V88 = c(44854.82049,
0.21, -0.7, 0, 0, 1403.07), V89 = c(44854.82083, 0.29, -1.03,
0.66, 0.71, 1404.35), V90 = c(44854.82118, 0.37, -1.03, 0.85,
0.91, 1405), V91 = c(44854.82153, 0.41, -0.97, 1.88, 1.94,
1406.58), V92 = c(44854.82188, 0.49, -1.03, 1.86, 1.92, 1406.93
), V93 = c(44854.82222, 0.53, -1.03, 2.12, 2.18, 1407.48),
V94 = c(44854.82257, 0.61, -1.03, 1.91, 1.98, 1407.59), V95 = c(44854.82292,
0.73, -1.03, 1.69, 1.76, 1407.87), V96 = c(44854.82326, 0.81,
-1.03, 1.92, 2, 1408.58), V97 = c(44854.82361, 0.93, -0.97,
1.74, 1.82, 1408.91), V98 = c(44854.82396, 1.05, -0.97, 1.75,
1.84, 1409.5), V99 = c(44854.82431, 1.13, -1.03, 1.71, 1.81,
1409.82), V100 = c(44854.82465, 1.25, -1.03, 1.72, 1.82,
1410.41), V101 = c(44854825, 1.36, -1.03, 1.68, 1.79, 1410.92
), V102 = c(44854.82535, 1.48, -0.97, 1.55, 1.67, 1411.32
), V103 = c(44854.82569, 1.64, -0.92, 0.5, 0.57, 1410.68),
V104 = c(44854.82604, 1.76, -0.92, 0.02, 0.03, 1410.61),
V105 = c(44854.82639, 1.87, -1.03, 0.01, 0.01, 1411.14),
V106 = c(44854.82674, 2.03, -0.97, 0, 0, 1411.86), V107 = c(44854.82708,
2.18, -0.81, 0, 0, 1412.6), V108 = c(44854.82743, 2.41, -0.86,
0, 0, 1413.69), V109 = c(44854.82778, 2.57, -0.86, 0, 0,
1414.4), V110 = c(44854.82813, 2.8, -0.92, 0, 0, 1415.47),
V111 = c(44854.82847, 3.1, -0.92, 0, 0, 1416.88), V112 = c(44854.82882,
3.4, -1.03, 1.91, 2.15, 1420.76)), class = "data.frame", row.names = c("Date_Time",
"Temp_C", "Depth_m", "Salinity_psu", "Conduct_mScm", "Sound.Velocity_msec"
))
>

Dummy data:
list <- list()
list[[1]] <- data.frame(column_x = rnorm(5,0,1), V2 = rnorm(5,0,1))
list[[2]] <- data.frame(column_x = rnorm(5,0,1), V2 = rnorm(5,0,1))
Correct loop:
for(i in 1:length(list)){
list[[i]] <- list[[i]][list[[i]]$column_x > 0,]
}
Although I'm pretty sure there has to be a more elegant solution using packages for lists like purrr.

Related

Selecting a row of data by closest time in R

In this dataset I want to get the entire row for a corresponding sample moment. Say the sample moment is t=100, I want the corresponding row of data. However, the time column does not consist of neat numbers, and I have a measurement at t = 99.7 and t= 101.1 (or something like that).
How do i get the samplemoment closest to t=100 out of my dataset?
Something like:
samplemoment = 100
test_samplemoment = test[samplemoment,]
However, since my measurements are every 2 seconds instead of every second. I'd need to give a correction:
test_samplemoment = test[(samplemoment/2),]
This does not reliably give the row of data closest to t=100 however, and I can't really come up with a better alternative that is reliable with different datasets like this.
I have the following dataset called test:
test <- structure(list(time = c(2, 3.9, 5.8, 7.8, 9.7, 11.7, 13.6, 15.5,
17.5, 19.4, 21.4, 23.3, 25.3, 27.2, 29.2, 31.2, 33.1, 35.1, 37.1,
39, 41, 42.9, 44.9, 46.8, 48.7, 50.7, 52.6, 54.6, 56.5, 58.5,
60.4, 62.4, 64.3, 66.2, 68.2, 70.1, 72.1, 74, 76, 77.9, 79.8,
81.8, 83.7, 85.7, 87.6, 89.5, 91.5, 93.4, 95.4, 97.3, 99.3, 101.2,
103.1, 105.1, 107, 109, 110.9, 112.8, 114.8, 116.7, 118.7, 120.6,
122.5, 124.5, 126.4, 128.4, 130.3, 132.2, 134.2, 136.1, 138.1,
140, 141.9, 143.9, 145.8, 147.8, 149.7, 151.6, 153.6, 155.5,
157.5, 159.4, 161.3, 163.3, 165.2, 167.2, 169.1, 171, 173, 174.9,
176.9, 178.8, 180.7, 182.7, 184.6, 186.6, 188.5, 190.4, 192.4,
194.3, 196.3, 198.2, 200.1, 202.1, 204, 206, 207.9, 209.8, 211.8,
213.7, 215.7, 217.6, 219.5, 221.5, 223.4, 225.4, 227.3, 229.3,
231.2, 233.1, 235.1, 237, 239, 240.9, 242.8, 244.8, 246.7, 248.7,
250.6, 252.5, 254.5, 256.4, 258.4, 260.3, 262.2, 264.2, 266.1,
268.1, 270, 271.9, 273.9, 275.8, 277.8, 279.7, 281.6, 283.6,
285.5, 287.5, 289.4, 291.3, 293.3, 295.2, 297.2, 299.1, 301),
v = c(14.82, 14.804, 14.82, 14.82, 14.804, 14.82, 14.812,
14.804, 14.8, 14.808, 14.8, 14.804, 15.844, 15.848, 15.848,
15.852, 15.852, 15.848, 15.852, 15.852, 15.852, 15.852, 15.856,
15.852, 15.852, 15.856, 15.856, 15.856, 15.856, 15.856, 15.856,
15.856, 15.852, 15.852, 15.852, 15.852, 15.856, 15.856, 15.856,
15.86, 15.856, 15.86, 15.864, 15.856, 15.86, 15.86, 15.86,
15.856, 15.86, 15.856, 15.86, 15.86, 15.856, 15.856, 15.86,
15.86, 15.86, 15.86, 15.864, 15.86, 15.86, 15.86, 15.86,
15.86, 15.856, 15.856, 15.856, 15.856, 15.856, 15.86, 15.86,
15.86, 15.856, 15.864, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.856, 15.856, 15.86, 15.86, 15.864, 15.86, 15.86,
15.86, 15.864, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86, 15.856,
15.86, 15.86, 15.86, 15.864, 15.86, 15.86, 15.86, 15.856,
15.86, 15.856, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.856, 15.86,
15.856, 15.86, 15.856, 15.86, 15.86, 15.864, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86, 15.86,
15.856, 15.86, 15.86), a = c(1.5, 1.476, 1.5, 1.491, 1.452,
1.476, 1.478, 1.44, 1.454, 1.438, 1.442, 1.471, 0.002, 0.002,
0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001, 0.002, 0.002,
0.001, 0.001, 0.002, 0.002, 0.002, 0.001, 0.001, 0.001, 0.002,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.002, 0.002,
0.002, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.002,
0.001, 0.001, 0.001, 0.002, 0.002, 0.001, 0.002, 0.001, 0.002,
0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.001, 0.002, 0.001, 0.001, 0.002, 0.002, 0.002, 0.002,
0.001, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002,
0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002,
0.002, 0.001, 0.001, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002,
0.001, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.001, 0.002,
0.002, 0.002, 0.001, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002,
0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002), t1 = c(0, -0.0120000000000005,
-0.0200000000000014, -0.0380000000000003, -0.0400000000000009,
-0.0200000000000014, -0.0300000000000011, 0.0199999999999996,
0.00999999999999979, 0.0080000000000009, 0.00999999999999979,
0.00200000000000067, 0.0199999999999996, 0.0699999999999985,
0.0800000000000001, 0.15, 0.17, 0.198, 0.219999999999999,
0.242000000000001, 0.279999999999999, 0.34, 0.369999999999999,
0.450000000000001, 0.48, 0.508000000000001, 0.539999999999999,
0.552, 0.58, 0.629999999999999, 0.649999999999999, 0.720000000000001,
0.73, 0.747999999999999, 0.76, 0.762, 0.779999999999999,
0.819999999999999, 0.82, 0.880000000000001, 0.880000000000001,
0.888, 0.889999999999999, 0.882, 0.889999999999999, 0.92,
0.92, 0.960000000000001, 0.960000000000001, 0.958, 0.949999999999999,
0.942, 0.94, 0.969999999999999, 0.959999999999999, 1, 1,
0.988, 0.979999999999999, 0.962, 0.969999999999999, 0.989999999999998,
0.979999999999999, 1.02, 1.01, 0.997999999999999, 0.989999999999998,
0.972, 0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1.01, 0.997999999999999, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1.01, 0.997999999999999, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.969999999999999,
1.01, 1, 0.988, 0.979999999999999, 0.962, 0.959999999999999,
0.979999999999999, 0.959999999999999, 1, 0.99, 0.978, 0.969999999999999,
0.952, 0.949999999999999, 0.959999999999999, 0.949999999999999,
0.99, 0.98, 0.968, 0.959999999999999, 0.932, 0.93, 0.949999999999999,
0.94, 0.98, 0.970000000000001, 0.958, 0.94, 0.922000000000001,
0.92, 0.94, 0.93, 0.960000000000001, 0.950000000000001, 0.938000000000001,
0.93, 0.912000000000001, 0.91, 0.92, 0.91, 0.950000000000001,
0.940000000000001, 0.928000000000001, 0.909999999999998,
0.891999999999999, 0.889999999999999, 0.909999999999998,
0.899999999999999, 0.94, 0.92, 0.907999999999999, 0.899999999999999,
0.882, 0.879999999999999, 0.899999999999999, 0.879999999999999,
0.92, 0.91, 0.898, 0.889999999999999, 0.862, 0.859999999999999,
0.879999999999999, 0.869999999999999, 0.91, 0.9, 0.878, 0.869999999999999
), t2 = c(-0.0179999999999989, -0.0300000000000011, -0.0100000000000016,
-0.0200000000000014, 0.0199999999999996, 0.00999999999999979,
-0.00200000000000067, 0, -0.0179999999999989, -0.00999999999999979,
0.0199999999999996, 0.0299999999999994, 0.0899999999999999,
0.100000000000001, 0.118, 0.149999999999999, 0.172000000000001,
0.209999999999999, 0.27, 0.309999999999999, 0.4, 0.450000000000001,
0.488, 0.529999999999999, 0.572000000000001, 0.619999999999999,
0.68, 0.719999999999999, 0.800000000000001, 0.83, 0.858000000000001,
0.879999999999999, 0.902000000000001, 0.93, 0.979999999999999,
0.99, 1.05, 1.07, 1.078, 1.08, 1.082, 1.1, 1.13, 1.13, 1.18,
1.18, 1.178, 1.18, 1.172, 1.18, 1.2, 1.2, 1.25, 1.24, 1.228,
1.23, 1.212, 1.22, 1.24, 1.23, 1.27, 1.26, 1.258, 1.25, 1.232,
1.23, 1.25, 1.24, 1.28, 1.27, 1.258, 1.25, 1.232, 1.23, 1.25,
1.24, 1.28, 1.27, 1.248, 1.24, 1.222, 1.22, 1.24, 1.23, 1.27,
1.26, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.25, 1.24, 1.228,
1.22, 1.202, 1.19, 1.21, 1.2, 1.24, 1.23, 1.208, 1.2, 1.182,
1.18, 1.19, 1.18, 1.22, 1.21, 1.198, 1.18, 1.162, 1.16, 1.18,
1.16, 1.2, 1.19, 1.178, 1.16, 1.142, 1.14, 1.16, 1.14, 1.18,
1.17, 1.158, 1.14, 1.122, 1.12, 1.14, 1.12, 1.16, 1.15, 1.138,
1.12, 1.102, 1.1, 1.12, 1.1, 1.14, 1.13, 1.108, 1.1, 1.082,
1.08, 1.09, 1.08, 1.12, 1.11, 1.088, 1.08, 1.062, 1.06, 1.07
), t3 = c(-0.00999999999999979, 0.0300000000000011, 0.0200000000000014,
0.0080000000000009, 0, -0.0179999999999989, -0.0199999999999996,
0, 0, 0.0500000000000007, 0.0500000000000007, 0.0579999999999998,
0.0699999999999985, 0.0820000000000007, 0.0999999999999996,
0.159999999999998, 0.19, 0.26, 0.300000000000001, 0.327999999999999,
0.369999999999999, 0.391999999999999, 0.44, 0.5, 0.539999999999999,
0.620000000000001, 0.65, 0.678000000000001, 0.699999999999999,
0.722, 0.75, 0.799999999999999, 0.82, 0.880000000000001,
0.890000000000001, 0.907999999999999, 0.909999999999998,
0.912000000000001, 0.93, 0.959999999999999, 0.969999999999999,
1.02, 1.02, 1.018, 1.02, 1.012, 1.02, 1.04, 1.04, 1.09, 1.08,
1.078, 1.07, 1.052, 1.06, 1.08, 1.07, 1.11, 1.11, 1.098,
1.09, 1.072, 1.07, 1.09, 1.08, 1.12, 1.11, 1.098, 1.09, 1.072,
1.07, 1.09, 1.08, 1.12, 1.11, 1.088, 1.08, 1.062, 1.06, 1.08,
1.07, 1.11, 1.1, 1.078, 1.07, 1.052, 1.05, 1.07, 1.06, 1.09,
1.08, 1.068, 1.06, 1.042, 1.03, 1.05, 1.04, 1.08, 1.07, 1.048,
1.04, 1.022, 1.02, 1.04, 1.02, 1.06, 1.05, 1.038, 1.03, 1.002,
1, 1.02, 1.01, 1.05, 1.03, 1.018, 1.01, 0.992000000000001,
0.99, 1, 0.99, 1.03, 1.02, 0.997999999999999, 0.989999999999998,
0.972, 0.969999999999999, 0.989999999999998, 0.979999999999999,
1.01, 1, 0.988, 0.979999999999999, 0.952, 0.949999999999999,
0.969999999999999, 0.959999999999999, 1, 0.98, 0.968, 0.959999999999999,
0.942, 0.93, 0.949999999999999, 0.94, 0.98, 0.970000000000001,
0.948, 0.94, 0.922000000000001, 0.92, 0.94, 0.92, 0.960000000000001,
0.950000000000001), t4 = c(0.0280000000000005, 0.0199999999999996,
0.00200000000000067, 0, 0.00999999999999979, 0, 0.0500000000000007,
0.0400000000000009, 0.0380000000000003, 0.0399999999999991,
0.032, 0.0499999999999989, 0.0999999999999996, 0.109999999999999,
0.19, 0.210000000000001, 0.238, 0.279999999999999, 0.302,
0.35, 0.42, 0.459999999999999, 0.56, 0.600000000000001, 0.628,
0.67, 0.702, 0.74, 0.799999999999999, 0.82, 0.9, 0.92, 0.938000000000001,
0.959999999999999, 0.972, 0.99, 1.03, 1.04, 1.1, 1.11, 1.108,
1.12, 1.112, 1.12, 1.15, 1.15, 1.2, 1.2, 1.198, 1.19, 1.182,
1.18, 1.21, 1.2, 1.24, 1.24, 1.228, 1.22, 1.212, 1.21, 1.23,
1.22, 1.26, 1.25, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.26,
1.25, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.26, 1.24, 1.228,
1.22, 1.202, 1.2, 1.22, 1.2, 1.24, 1.23, 1.218, 1.21, 1.192,
1.18, 1.2, 1.19, 1.23, 1.22, 1.198, 1.19, 1.172, 1.17, 1.19,
1.17, 1.21, 1.2, 1.188, 1.18, 1.152, 1.15, 1.17, 1.16, 1.19,
1.18, 1.168, 1.16, 1.142, 1.13, 1.15, 1.14, 1.18, 1.16, 1.148,
1.14, 1.122, 1.11, 1.13, 1.12, 1.16, 1.14, 1.128, 1.12, 1.102,
1.09, 1.11, 1.1, 1.14, 1.12, 1.108, 1.1, 1.082, 1.07, 1.09,
1.08, 1.12, 1.1, 1.088, 1.08, 1.062, 1.06, 1.07, 1.06, 1.1,
1.09, 1.068, 1.06, 1.042), t5 = c(0, 0.0199999999999996,
0.00999999999999979, 0.0500000000000007, 0.0400000000000009,
0.0280000000000005, 0.0299999999999994, 0.0120000000000005,
0.0199999999999996, 0.0599999999999987, 0.0599999999999987,
0.130000000000001, 0.140000000000001, 0.157999999999999,
0.18, 0.202, 0.24, 0.299999999999999, 0.33, 0.42, 0.450000000000001,
0.488, 0.52, 0.552, 0.59, 0.649999999999999, 0.68, 0.76,
0.790000000000001, 0.808, 0.83, 0.842000000000001, 0.869999999999999,
0.909999999999998, 0.93, 0.99, 1, 1.008, 1.01, 1.012, 1.03,
1.06, 1.06, 1.11, 1.11, 1.108, 1.11, 1.102, 1.11, 1.13, 1.13,
1.17, 1.17, 1.158, 1.16, 1.142, 1.14, 1.17, 1.16, 1.2, 1.19,
1.178, 1.17, 1.162, 1.16, 1.18, 1.17, 1.21, 1.2, 1.188, 1.18,
1.162, 1.16, 1.17, 1.16, 1.2, 1.19, 1.178, 1.17, 1.152, 1.15,
1.17, 1.15, 1.19, 1.18, 1.168, 1.16, 1.142, 1.14, 1.15, 1.14,
1.18, 1.17, 1.158, 1.14, 1.122, 1.12, 1.14, 1.13, 1.17, 1.15,
1.138, 1.13, 1.112, 1.1, 1.12, 1.11, 1.15, 1.14, 1.118, 1.11,
1.092, 1.09, 1.11, 1.09, 1.13, 1.12, 1.108, 1.09, 1.072,
1.07, 1.09, 1.08, 1.11, 1.1, 1.088, 1.08, 1.062, 1.05, 1.07,
1.06, 1.1, 1.08, 1.068, 1.06, 1.042, 1.03, 1.05, 1.04, 1.08,
1.07, 1.048, 1.04, 1.022, 1.02, 1.03, 1.02, 1.06, 1.05, 1.038,
1.02, 1.002, 1, 1.02, 1), t6 = c(0.0300000000000011, 0.0200000000000014,
0.0080000000000009, 0, -0.0179999999999989, -0.0199999999999996,
0, 0, 0.0500000000000007, 0.0500000000000007, 0.048, 0.0599999999999987,
0.072000000000001, 0.0899999999999999, 0.139999999999999,
0.16, 0.24, 0.270000000000001, 0.288, 0.319999999999999,
0.352, 0.389999999999999, 0.449999999999999, 0.479999999999999,
0.56, 0.59, 0.608000000000001, 0.639999999999999, 0.652000000000001,
0.68, 0.729999999999999, 0.75, 0.81, 0.83, 0.838000000000001,
0.85, 0.852, 0.869999999999999, 0.909999999999998, 0.91,
0.970000000000001, 0.970000000000001, 0.968, 0.979999999999999,
0.972, 0.98, 1.01, 1.01, 1.05, 1.05, 1.048, 1.04, 1.032,
1.04, 1.06, 1.06, 1.1, 1.09, 1.088, 1.08, 1.062, 1.06, 1.09,
1.08, 1.12, 1.11, 1.098, 1.1, 1.082, 1.08, 1.1, 1.09, 1.13,
1.12, 1.108, 1.1, 1.082, 1.08, 1.1, 1.09, 1.13, 1.12, 1.108,
1.1, 1.082, 1.07, 1.09, 1.08, 1.12, 1.11, 1.098, 1.09, 1.072,
1.07, 1.08, 1.07, 1.11, 1.1, 1.088, 1.08, 1.062, 1.06, 1.07,
1.06, 1.1, 1.09, 1.078, 1.07, 1.042, 1.04, 1.06, 1.05, 1.09,
1.07, 1.058, 1.05, 1.032, 1.03, 1.05, 1.03, 1.07, 1.06, 1.048,
1.04, 1.012, 1.01, 1.03, 1.02, 1.06, 1.04, 1.028, 1.02, 1.002,
1, 1.01, 1, 1.04, 1.03, 1.018, 1, 0.982000000000001, 0.98,
1, 0.99, 1.02, 1.01, 0.997999999999999, 0.989999999999998,
0.972, 0.959999999999999, 0.979999999999999, 0.969999999999999,
1.01, 1, 0.978), t7 = c(0.00999999999999979, -0.00799999999999912,
-0.00999999999999979, 0.00999999999999979, 0, 0.0400000000000009,
0.0300000000000011, 0.0180000000000007, 0.0199999999999996,
0.0120000000000005, 0.0199999999999996, 0.0499999999999989,
0.0599999999999987, 0.120000000000001, 0.140000000000001,
0.148, 0.17, 0.182, 0.209999999999999, 0.27, 0.289999999999999,
0.370000000000001, 0.390000000000001, 0.417999999999999,
0.44, 0.462, 0.49, 0.539999999999999, 0.57, 0.640000000000001,
0.65, 0.667999999999999, 0.69, 0.692, 0.709999999999999,
0.75, 0.77, 0.82, 0.83, 0.838000000000001, 0.84, 0.842000000000001,
0.85, 0.889999999999999, 0.889999999999999, 0.94, 0.940000000000001,
0.938000000000001, 0.94, 0.932, 0.94, 0.969999999999999,
0.969999999999999, 1.01, 1.01, 0.997999999999999, 1, 0.992000000000001,
0.99, 1.02, 1.01, 1.05, 1.05, 1.038, 1.03, 1.022, 1.02, 1.04,
1.04, 1.08, 1.07, 1.058, 1.05, 1.032, 1.04, 1.06, 1.05, 1.09,
1.08, 1.068, 1.06, 1.042, 1.04, 1.06, 1.05, 1.09, 1.08, 1.068,
1.06, 1.042, 1.04, 1.06, 1.05, 1.09, 1.08, 1.068, 1.06, 1.042,
1.04, 1.06, 1.05, 1.09, 1.08, 1.068, 1.06, 1.032, 1.03, 1.05,
1.04, 1.08, 1.07, 1.058, 1.05, 1.022, 1.02, 1.04, 1.03, 1.07,
1.06, 1.048, 1.04, 1.012, 1.01, 1.03, 1.02, 1.06, 1.05, 1.028,
1.02, 1.002, 1, 1.02, 1.01, 1.04, 1.03, 1.018, 1.01, 0.992000000000001,
0.99, 1, 0.99, 1.03, 1.02, 1.008, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1, 0.988, 0.979999999999999, 0.962, 0.959999999999999
), t8 = c(-0.0300000000000011, -0.0400000000000009, 0, -0.00999999999999979,
-0.0220000000000002, -0.0300000000000011, -0.0380000000000003,
-0.0400000000000009, -0.0100000000000016, -0.00999999999999979,
0.0400000000000009, 0.0500000000000007, 0.0579999999999998,
0.0800000000000001, 0.0920000000000005, 0.119999999999999,
0.17, 0.199999999999999, 0.280000000000001, 0.31, 0.338000000000001,
0.369999999999999, 0.382, 0.42, 0.479999999999999, 0.5, 0.57,
0.600000000000001, 0.618, 0.629999999999999, 0.641999999999999,
0.66, 0.709999999999999, 0.719999999999999, 0.780000000000001,
0.780000000000001, 0.788, 0.799999999999999, 0.792, 0.799999999999999,
0.84, 0.84, 0.890000000000001, 0.890000000000001, 0.888,
0.889999999999999, 0.872, 0.879999999999999, 0.909999999999998,
0.899999999999999, 0.950000000000001, 0.940000000000001,
0.938000000000001, 0.93, 0.922000000000001, 0.92, 0.94, 0.94,
0.98, 0.970000000000001, 0.958, 0.959999999999999, 0.942,
0.94, 0.959999999999999, 0.949999999999999, 0.99, 0.98, 0.968,
0.969999999999999, 0.952, 0.949999999999999, 0.969999999999999,
0.959999999999999, 1, 0.99, 0.978, 0.969999999999999, 0.952,
0.949999999999999, 0.959999999999999, 0.949999999999999,
0.99, 0.98, 0.968, 0.959999999999999, 0.942, 0.94, 0.959999999999999,
0.949999999999999, 0.99, 0.98, 0.958, 0.949999999999999,
0.932, 0.93, 0.949999999999999, 0.94, 0.98, 0.970000000000001,
0.958, 0.94, 0.922000000000001, 0.92, 0.94, 0.93, 0.970000000000001,
0.960000000000001, 0.938000000000001, 0.93, 0.912000000000001,
0.91, 0.93, 0.92, 0.950000000000001, 0.940000000000001, 0.928000000000001,
0.92, 0.902000000000001, 0.9, 0.909999999999998, 0.899999999999999,
0.94, 0.93, 0.917999999999999, 0.909999999999998, 0.891999999999999,
0.879999999999999, 0.899999999999999, 0.889999999999999,
0.93, 0.92, 0.907999999999999, 0.889999999999999, 0.872,
0.869999999999999, 0.889999999999999, 0.879999999999999,
0.92, 0.9, 0.888, 0.879999999999999, 0.862, 0.859999999999999,
0.869999999999999, 0.859999999999999, 0.9, 0.890000000000001,
0.878, 0.869999999999999, 0.852, 0.84, 0.859999999999999,
0.85, 0.890000000000001)), row.names = c(NA, -155L), class = "data.frame")
You can also use which.min() from base R.
test[which.min(abs(100-test$time)),]
time v a t1 t2 t3 t4 t5 t6 t7 t8
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
51 99.3 15.86 0.001 0.95 1.2 1.08 1.182 1.13 1.048 0.94 0.95
This will return a row from the test -dataframe that has a value closest to the value of 100 in the column time.
We could compare the absolut difference of time and 100 with the min of this difference:
library(dplyr)
test %>%
filter(abs(time-100)==min(abs(time-100)))
time v a t1 t2 t3 t4 t5 t6 t7 t8
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 99.3 15.9 0.001 0.950 1.2 1.08 1.18 1.13 1.05 0.94 0.950

Iterating a calculation over rows

I need to calculate the N variable for each row.
This is done by multiplying the values in the (horizontal) row with the vector Nangle (which is of the same length as test[,c(4:11)]) and getting the sum of these 8 multiplications. It works for a single row as follows
sum(test[1,c(4:11)] * Nangle)
However, I need to do this calculation for every row, and add the results as a new (vertical) column 'N'
I tried the following:
test$N = sum(test[,c(4:11)] * Nangle)
This does create the new column, but just adds the same number after every row, so somehow the calculation is only done for the total of all of the rows and not per row.
How do I make this iterate over all rows?
The dataset test:
test <- structure(list(time = c(2, 3.9, 5.8, 7.8, 9.7, 11.7, 13.6, 15.5,
17.5, 19.4, 21.4, 23.3, 25.3, 27.2, 29.2, 31.2, 33.1, 35.1, 37.1,
39, 41, 42.9, 44.9, 46.8, 48.7, 50.7, 52.6, 54.6, 56.5, 58.5,
60.4, 62.4, 64.3, 66.2, 68.2, 70.1, 72.1, 74, 76, 77.9, 79.8,
81.8, 83.7, 85.7, 87.6, 89.5, 91.5, 93.4, 95.4, 97.3, 99.3, 101.2,
103.1, 105.1, 107, 109, 110.9, 112.8, 114.8, 116.7, 118.7, 120.6,
122.5, 124.5, 126.4, 128.4, 130.3, 132.2, 134.2, 136.1, 138.1,
140, 141.9, 143.9, 145.8, 147.8, 149.7, 151.6, 153.6, 155.5,
157.5, 159.4, 161.3, 163.3, 165.2, 167.2, 169.1, 171, 173, 174.9,
176.9, 178.8, 180.7, 182.7, 184.6, 186.6, 188.5, 190.4, 192.4,
194.3, 196.3, 198.2, 200.1, 202.1, 204, 206, 207.9, 209.8, 211.8,
213.7, 215.7, 217.6, 219.5, 221.5, 223.4, 225.4, 227.3, 229.3,
231.2, 233.1, 235.1, 237, 239, 240.9, 242.8, 244.8, 246.7, 248.7,
250.6, 252.5, 254.5, 256.4, 258.4, 260.3, 262.2, 264.2, 266.1,
268.1, 270, 271.9, 273.9, 275.8, 277.8, 279.7, 281.6, 283.6,
285.5, 287.5, 289.4, 291.3, 293.3, 295.2, 297.2, 299.1, 301),
v = c(14.82, 14.804, 14.82, 14.82, 14.804, 14.82, 14.812,
14.804, 14.8, 14.808, 14.8, 14.804, 15.844, 15.848, 15.848,
15.852, 15.852, 15.848, 15.852, 15.852, 15.852, 15.852, 15.856,
15.852, 15.852, 15.856, 15.856, 15.856, 15.856, 15.856, 15.856,
15.856, 15.852, 15.852, 15.852, 15.852, 15.856, 15.856, 15.856,
15.86, 15.856, 15.86, 15.864, 15.856, 15.86, 15.86, 15.86,
15.856, 15.86, 15.856, 15.86, 15.86, 15.856, 15.856, 15.86,
15.86, 15.86, 15.86, 15.864, 15.86, 15.86, 15.86, 15.86,
15.86, 15.856, 15.856, 15.856, 15.856, 15.856, 15.86, 15.86,
15.86, 15.856, 15.864, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.856, 15.856, 15.86, 15.86, 15.864, 15.86, 15.86,
15.86, 15.864, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86, 15.856,
15.86, 15.86, 15.86, 15.864, 15.86, 15.86, 15.86, 15.856,
15.86, 15.856, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.856, 15.86,
15.856, 15.86, 15.856, 15.86, 15.86, 15.864, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86, 15.86,
15.856, 15.86, 15.86), a = c(1.5, 1.476, 1.5, 1.491, 1.452,
1.476, 1.478, 1.44, 1.454, 1.438, 1.442, 1.471, 0.002, 0.002,
0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001, 0.002, 0.002,
0.001, 0.001, 0.002, 0.002, 0.002, 0.001, 0.001, 0.001, 0.002,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.002, 0.002,
0.002, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.002,
0.001, 0.001, 0.001, 0.002, 0.002, 0.001, 0.002, 0.001, 0.002,
0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.001, 0.002, 0.001, 0.001, 0.002, 0.002, 0.002, 0.002,
0.001, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002,
0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002,
0.002, 0.001, 0.001, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002,
0.001, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.001, 0.002,
0.002, 0.002, 0.001, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002,
0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002), t1 = c(0, -0.0120000000000005,
-0.0200000000000014, -0.0380000000000003, -0.0400000000000009,
-0.0200000000000014, -0.0300000000000011, 0.0199999999999996,
0.00999999999999979, 0.0080000000000009, 0.00999999999999979,
0.00200000000000067, 0.0199999999999996, 0.0699999999999985,
0.0800000000000001, 0.15, 0.17, 0.198, 0.219999999999999,
0.242000000000001, 0.279999999999999, 0.34, 0.369999999999999,
0.450000000000001, 0.48, 0.508000000000001, 0.539999999999999,
0.552, 0.58, 0.629999999999999, 0.649999999999999, 0.720000000000001,
0.73, 0.747999999999999, 0.76, 0.762, 0.779999999999999,
0.819999999999999, 0.82, 0.880000000000001, 0.880000000000001,
0.888, 0.889999999999999, 0.882, 0.889999999999999, 0.92,
0.92, 0.960000000000001, 0.960000000000001, 0.958, 0.949999999999999,
0.942, 0.94, 0.969999999999999, 0.959999999999999, 1, 1,
0.988, 0.979999999999999, 0.962, 0.969999999999999, 0.989999999999998,
0.979999999999999, 1.02, 1.01, 0.997999999999999, 0.989999999999998,
0.972, 0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1.01, 0.997999999999999, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1.01, 0.997999999999999, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.969999999999999,
1.01, 1, 0.988, 0.979999999999999, 0.962, 0.959999999999999,
0.979999999999999, 0.959999999999999, 1, 0.99, 0.978, 0.969999999999999,
0.952, 0.949999999999999, 0.959999999999999, 0.949999999999999,
0.99, 0.98, 0.968, 0.959999999999999, 0.932, 0.93, 0.949999999999999,
0.94, 0.98, 0.970000000000001, 0.958, 0.94, 0.922000000000001,
0.92, 0.94, 0.93, 0.960000000000001, 0.950000000000001, 0.938000000000001,
0.93, 0.912000000000001, 0.91, 0.92, 0.91, 0.950000000000001,
0.940000000000001, 0.928000000000001, 0.909999999999998,
0.891999999999999, 0.889999999999999, 0.909999999999998,
0.899999999999999, 0.94, 0.92, 0.907999999999999, 0.899999999999999,
0.882, 0.879999999999999, 0.899999999999999, 0.879999999999999,
0.92, 0.91, 0.898, 0.889999999999999, 0.862, 0.859999999999999,
0.879999999999999, 0.869999999999999, 0.91, 0.9, 0.878, 0.869999999999999
), t2 = c(-0.0179999999999989, -0.0300000000000011, -0.0100000000000016,
-0.0200000000000014, 0.0199999999999996, 0.00999999999999979,
-0.00200000000000067, 0, -0.0179999999999989, -0.00999999999999979,
0.0199999999999996, 0.0299999999999994, 0.0899999999999999,
0.100000000000001, 0.118, 0.149999999999999, 0.172000000000001,
0.209999999999999, 0.27, 0.309999999999999, 0.4, 0.450000000000001,
0.488, 0.529999999999999, 0.572000000000001, 0.619999999999999,
0.68, 0.719999999999999, 0.800000000000001, 0.83, 0.858000000000001,
0.879999999999999, 0.902000000000001, 0.93, 0.979999999999999,
0.99, 1.05, 1.07, 1.078, 1.08, 1.082, 1.1, 1.13, 1.13, 1.18,
1.18, 1.178, 1.18, 1.172, 1.18, 1.2, 1.2, 1.25, 1.24, 1.228,
1.23, 1.212, 1.22, 1.24, 1.23, 1.27, 1.26, 1.258, 1.25, 1.232,
1.23, 1.25, 1.24, 1.28, 1.27, 1.258, 1.25, 1.232, 1.23, 1.25,
1.24, 1.28, 1.27, 1.248, 1.24, 1.222, 1.22, 1.24, 1.23, 1.27,
1.26, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.25, 1.24, 1.228,
1.22, 1.202, 1.19, 1.21, 1.2, 1.24, 1.23, 1.208, 1.2, 1.182,
1.18, 1.19, 1.18, 1.22, 1.21, 1.198, 1.18, 1.162, 1.16, 1.18,
1.16, 1.2, 1.19, 1.178, 1.16, 1.142, 1.14, 1.16, 1.14, 1.18,
1.17, 1.158, 1.14, 1.122, 1.12, 1.14, 1.12, 1.16, 1.15, 1.138,
1.12, 1.102, 1.1, 1.12, 1.1, 1.14, 1.13, 1.108, 1.1, 1.082,
1.08, 1.09, 1.08, 1.12, 1.11, 1.088, 1.08, 1.062, 1.06, 1.07
), t3 = c(-0.00999999999999979, 0.0300000000000011, 0.0200000000000014,
0.0080000000000009, 0, -0.0179999999999989, -0.0199999999999996,
0, 0, 0.0500000000000007, 0.0500000000000007, 0.0579999999999998,
0.0699999999999985, 0.0820000000000007, 0.0999999999999996,
0.159999999999998, 0.19, 0.26, 0.300000000000001, 0.327999999999999,
0.369999999999999, 0.391999999999999, 0.44, 0.5, 0.539999999999999,
0.620000000000001, 0.65, 0.678000000000001, 0.699999999999999,
0.722, 0.75, 0.799999999999999, 0.82, 0.880000000000001,
0.890000000000001, 0.907999999999999, 0.909999999999998,
0.912000000000001, 0.93, 0.959999999999999, 0.969999999999999,
1.02, 1.02, 1.018, 1.02, 1.012, 1.02, 1.04, 1.04, 1.09, 1.08,
1.078, 1.07, 1.052, 1.06, 1.08, 1.07, 1.11, 1.11, 1.098,
1.09, 1.072, 1.07, 1.09, 1.08, 1.12, 1.11, 1.098, 1.09, 1.072,
1.07, 1.09, 1.08, 1.12, 1.11, 1.088, 1.08, 1.062, 1.06, 1.08,
1.07, 1.11, 1.1, 1.078, 1.07, 1.052, 1.05, 1.07, 1.06, 1.09,
1.08, 1.068, 1.06, 1.042, 1.03, 1.05, 1.04, 1.08, 1.07, 1.048,
1.04, 1.022, 1.02, 1.04, 1.02, 1.06, 1.05, 1.038, 1.03, 1.002,
1, 1.02, 1.01, 1.05, 1.03, 1.018, 1.01, 0.992000000000001,
0.99, 1, 0.99, 1.03, 1.02, 0.997999999999999, 0.989999999999998,
0.972, 0.969999999999999, 0.989999999999998, 0.979999999999999,
1.01, 1, 0.988, 0.979999999999999, 0.952, 0.949999999999999,
0.969999999999999, 0.959999999999999, 1, 0.98, 0.968, 0.959999999999999,
0.942, 0.93, 0.949999999999999, 0.94, 0.98, 0.970000000000001,
0.948, 0.94, 0.922000000000001, 0.92, 0.94, 0.92, 0.960000000000001,
0.950000000000001), t4 = c(0.0280000000000005, 0.0199999999999996,
0.00200000000000067, 0, 0.00999999999999979, 0, 0.0500000000000007,
0.0400000000000009, 0.0380000000000003, 0.0399999999999991,
0.032, 0.0499999999999989, 0.0999999999999996, 0.109999999999999,
0.19, 0.210000000000001, 0.238, 0.279999999999999, 0.302,
0.35, 0.42, 0.459999999999999, 0.56, 0.600000000000001, 0.628,
0.67, 0.702, 0.74, 0.799999999999999, 0.82, 0.9, 0.92, 0.938000000000001,
0.959999999999999, 0.972, 0.99, 1.03, 1.04, 1.1, 1.11, 1.108,
1.12, 1.112, 1.12, 1.15, 1.15, 1.2, 1.2, 1.198, 1.19, 1.182,
1.18, 1.21, 1.2, 1.24, 1.24, 1.228, 1.22, 1.212, 1.21, 1.23,
1.22, 1.26, 1.25, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.26,
1.25, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.26, 1.24, 1.228,
1.22, 1.202, 1.2, 1.22, 1.2, 1.24, 1.23, 1.218, 1.21, 1.192,
1.18, 1.2, 1.19, 1.23, 1.22, 1.198, 1.19, 1.172, 1.17, 1.19,
1.17, 1.21, 1.2, 1.188, 1.18, 1.152, 1.15, 1.17, 1.16, 1.19,
1.18, 1.168, 1.16, 1.142, 1.13, 1.15, 1.14, 1.18, 1.16, 1.148,
1.14, 1.122, 1.11, 1.13, 1.12, 1.16, 1.14, 1.128, 1.12, 1.102,
1.09, 1.11, 1.1, 1.14, 1.12, 1.108, 1.1, 1.082, 1.07, 1.09,
1.08, 1.12, 1.1, 1.088, 1.08, 1.062, 1.06, 1.07, 1.06, 1.1,
1.09, 1.068, 1.06, 1.042), t5 = c(0, 0.0199999999999996,
0.00999999999999979, 0.0500000000000007, 0.0400000000000009,
0.0280000000000005, 0.0299999999999994, 0.0120000000000005,
0.0199999999999996, 0.0599999999999987, 0.0599999999999987,
0.130000000000001, 0.140000000000001, 0.157999999999999,
0.18, 0.202, 0.24, 0.299999999999999, 0.33, 0.42, 0.450000000000001,
0.488, 0.52, 0.552, 0.59, 0.649999999999999, 0.68, 0.76,
0.790000000000001, 0.808, 0.83, 0.842000000000001, 0.869999999999999,
0.909999999999998, 0.93, 0.99, 1, 1.008, 1.01, 1.012, 1.03,
1.06, 1.06, 1.11, 1.11, 1.108, 1.11, 1.102, 1.11, 1.13, 1.13,
1.17, 1.17, 1.158, 1.16, 1.142, 1.14, 1.17, 1.16, 1.2, 1.19,
1.178, 1.17, 1.162, 1.16, 1.18, 1.17, 1.21, 1.2, 1.188, 1.18,
1.162, 1.16, 1.17, 1.16, 1.2, 1.19, 1.178, 1.17, 1.152, 1.15,
1.17, 1.15, 1.19, 1.18, 1.168, 1.16, 1.142, 1.14, 1.15, 1.14,
1.18, 1.17, 1.158, 1.14, 1.122, 1.12, 1.14, 1.13, 1.17, 1.15,
1.138, 1.13, 1.112, 1.1, 1.12, 1.11, 1.15, 1.14, 1.118, 1.11,
1.092, 1.09, 1.11, 1.09, 1.13, 1.12, 1.108, 1.09, 1.072,
1.07, 1.09, 1.08, 1.11, 1.1, 1.088, 1.08, 1.062, 1.05, 1.07,
1.06, 1.1, 1.08, 1.068, 1.06, 1.042, 1.03, 1.05, 1.04, 1.08,
1.07, 1.048, 1.04, 1.022, 1.02, 1.03, 1.02, 1.06, 1.05, 1.038,
1.02, 1.002, 1, 1.02, 1), t6 = c(0.0300000000000011, 0.0200000000000014,
0.0080000000000009, 0, -0.0179999999999989, -0.0199999999999996,
0, 0, 0.0500000000000007, 0.0500000000000007, 0.048, 0.0599999999999987,
0.072000000000001, 0.0899999999999999, 0.139999999999999,
0.16, 0.24, 0.270000000000001, 0.288, 0.319999999999999,
0.352, 0.389999999999999, 0.449999999999999, 0.479999999999999,
0.56, 0.59, 0.608000000000001, 0.639999999999999, 0.652000000000001,
0.68, 0.729999999999999, 0.75, 0.81, 0.83, 0.838000000000001,
0.85, 0.852, 0.869999999999999, 0.909999999999998, 0.91,
0.970000000000001, 0.970000000000001, 0.968, 0.979999999999999,
0.972, 0.98, 1.01, 1.01, 1.05, 1.05, 1.048, 1.04, 1.032,
1.04, 1.06, 1.06, 1.1, 1.09, 1.088, 1.08, 1.062, 1.06, 1.09,
1.08, 1.12, 1.11, 1.098, 1.1, 1.082, 1.08, 1.1, 1.09, 1.13,
1.12, 1.108, 1.1, 1.082, 1.08, 1.1, 1.09, 1.13, 1.12, 1.108,
1.1, 1.082, 1.07, 1.09, 1.08, 1.12, 1.11, 1.098, 1.09, 1.072,
1.07, 1.08, 1.07, 1.11, 1.1, 1.088, 1.08, 1.062, 1.06, 1.07,
1.06, 1.1, 1.09, 1.078, 1.07, 1.042, 1.04, 1.06, 1.05, 1.09,
1.07, 1.058, 1.05, 1.032, 1.03, 1.05, 1.03, 1.07, 1.06, 1.048,
1.04, 1.012, 1.01, 1.03, 1.02, 1.06, 1.04, 1.028, 1.02, 1.002,
1, 1.01, 1, 1.04, 1.03, 1.018, 1, 0.982000000000001, 0.98,
1, 0.99, 1.02, 1.01, 0.997999999999999, 0.989999999999998,
0.972, 0.959999999999999, 0.979999999999999, 0.969999999999999,
1.01, 1, 0.978), t7 = c(0.00999999999999979, -0.00799999999999912,
-0.00999999999999979, 0.00999999999999979, 0, 0.0400000000000009,
0.0300000000000011, 0.0180000000000007, 0.0199999999999996,
0.0120000000000005, 0.0199999999999996, 0.0499999999999989,
0.0599999999999987, 0.120000000000001, 0.140000000000001,
0.148, 0.17, 0.182, 0.209999999999999, 0.27, 0.289999999999999,
0.370000000000001, 0.390000000000001, 0.417999999999999,
0.44, 0.462, 0.49, 0.539999999999999, 0.57, 0.640000000000001,
0.65, 0.667999999999999, 0.69, 0.692, 0.709999999999999,
0.75, 0.77, 0.82, 0.83, 0.838000000000001, 0.84, 0.842000000000001,
0.85, 0.889999999999999, 0.889999999999999, 0.94, 0.940000000000001,
0.938000000000001, 0.94, 0.932, 0.94, 0.969999999999999,
0.969999999999999, 1.01, 1.01, 0.997999999999999, 1, 0.992000000000001,
0.99, 1.02, 1.01, 1.05, 1.05, 1.038, 1.03, 1.022, 1.02, 1.04,
1.04, 1.08, 1.07, 1.058, 1.05, 1.032, 1.04, 1.06, 1.05, 1.09,
1.08, 1.068, 1.06, 1.042, 1.04, 1.06, 1.05, 1.09, 1.08, 1.068,
1.06, 1.042, 1.04, 1.06, 1.05, 1.09, 1.08, 1.068, 1.06, 1.042,
1.04, 1.06, 1.05, 1.09, 1.08, 1.068, 1.06, 1.032, 1.03, 1.05,
1.04, 1.08, 1.07, 1.058, 1.05, 1.022, 1.02, 1.04, 1.03, 1.07,
1.06, 1.048, 1.04, 1.012, 1.01, 1.03, 1.02, 1.06, 1.05, 1.028,
1.02, 1.002, 1, 1.02, 1.01, 1.04, 1.03, 1.018, 1.01, 0.992000000000001,
0.99, 1, 0.99, 1.03, 1.02, 1.008, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1, 0.988, 0.979999999999999, 0.962, 0.959999999999999
), t8 = c(-0.0300000000000011, -0.0400000000000009, 0, -0.00999999999999979,
-0.0220000000000002, -0.0300000000000011, -0.0380000000000003,
-0.0400000000000009, -0.0100000000000016, -0.00999999999999979,
0.0400000000000009, 0.0500000000000007, 0.0579999999999998,
0.0800000000000001, 0.0920000000000005, 0.119999999999999,
0.17, 0.199999999999999, 0.280000000000001, 0.31, 0.338000000000001,
0.369999999999999, 0.382, 0.42, 0.479999999999999, 0.5, 0.57,
0.600000000000001, 0.618, 0.629999999999999, 0.641999999999999,
0.66, 0.709999999999999, 0.719999999999999, 0.780000000000001,
0.780000000000001, 0.788, 0.799999999999999, 0.792, 0.799999999999999,
0.84, 0.84, 0.890000000000001, 0.890000000000001, 0.888,
0.889999999999999, 0.872, 0.879999999999999, 0.909999999999998,
0.899999999999999, 0.950000000000001, 0.940000000000001,
0.938000000000001, 0.93, 0.922000000000001, 0.92, 0.94, 0.94,
0.98, 0.970000000000001, 0.958, 0.959999999999999, 0.942,
0.94, 0.959999999999999, 0.949999999999999, 0.99, 0.98, 0.968,
0.969999999999999, 0.952, 0.949999999999999, 0.969999999999999,
0.959999999999999, 1, 0.99, 0.978, 0.969999999999999, 0.952,
0.949999999999999, 0.959999999999999, 0.949999999999999,
0.99, 0.98, 0.968, 0.959999999999999, 0.942, 0.94, 0.959999999999999,
0.949999999999999, 0.99, 0.98, 0.958, 0.949999999999999,
0.932, 0.93, 0.949999999999999, 0.94, 0.98, 0.970000000000001,
0.958, 0.94, 0.922000000000001, 0.92, 0.94, 0.93, 0.970000000000001,
0.960000000000001, 0.938000000000001, 0.93, 0.912000000000001,
0.91, 0.93, 0.92, 0.950000000000001, 0.940000000000001, 0.928000000000001,
0.92, 0.902000000000001, 0.9, 0.909999999999998, 0.899999999999999,
0.94, 0.93, 0.917999999999999, 0.909999999999998, 0.891999999999999,
0.879999999999999, 0.899999999999999, 0.889999999999999,
0.93, 0.92, 0.907999999999999, 0.889999999999999, 0.872,
0.869999999999999, 0.889999999999999, 0.879999999999999,
0.92, 0.9, 0.888, 0.879999999999999, 0.862, 0.859999999999999,
0.869999999999999, 0.859999999999999, 0.9, 0.890000000000001,
0.878, 0.869999999999999, 0.852, 0.84, 0.859999999999999,
0.85, 0.890000000000001)), row.names = c(NA, -155L), class = "data.frame")
Nangle <- c(1, 0.707106781186548, 6.12303176911189e-17, -0.707106781186547,
-1, -0.707106781186548, -1.83690953073357e-16, 0.707106781186547
)
I probably need to fill in something before the , between the brackets, but can't seem to figure out what exactly.
This works:
test$N <- colSums(t(test[, 4:11]) * Nangle)
However, I would be careful about using column numbers in the code when you can easily refer to the appropriate columns by their names, e.g. like this:
test$N <- colSums(t(test[, paste0('t', 1:8)]) * Nangle)

Clustering differences between pheatmap and corrplot() using ward.D2 method

I have performed Spearman correlation for my data. Then I tried to cluster and plot my data using the "ward.D2" method for corrplot()and pheatmap(). However, the order of the variables is different between the two plots.
Could someone help me clarify this point, thus correcting my code and creating the two plots with the same order of clustered variables?
Thank you so much.
#A sample of my dataset:
dput(Data_corr)
structure(list(S_cHDLP = c(0.299999999999999, -2.78, 0.880000000000001,
2.48, 2.15, 5.31, 3.02, 1.19, 2.1, -1.18, -0.34, 1.25, -3.25,
-3.16, 0.19, -0.100000000000001, -2.16, -0.220000000000001, 0.77,
-2.12), H7P = c(-0.18, -0.48, -0.13, -0.21, 0.07, 0.64, -0.13,
-0.1, 0.12, -0.22, 0.09, -0.0399999999999999, -1.56, 0.39, 0.58,
-0.49, 0.2, 0.13, 0.11, 0.06), H6P = c(0, 0, 0, 0.16, -0.23,
0, 0, 0, -0.26, -0.28, 0.06, -0.17, 1.16, -0.12, -0.32, -0.17,
0.38, 0.05, 0.01, 0), H5P = c(0, 0.84, 0.47, 1.21, 0.01, 0.21,
1.36, 0.2, -0.12, 0.93, -1.01, 0, -0.58, -0.97, -1, 0.97, -0.89,
0.35, -0.59, -0.12), H4P = c(-0.12, -1.27, -0.18, 0.25, 1.02,
1.26, -0.62, -0.16, 0.25, -0.01, 0.44, 0.17, 0.19, 0.97, 2.35,
0.3, -0.18, 0.03, 0.0899999999999999, 0.38), H3P = c(-0.31, 0.39,
0.13, 0.29, 0, 0.02, -0.07, 0, 0, -0.32, 0, -0.79, 0, -0.53,
-0.71, -0.2, 2.08, 0.86, 0, 0), H2P = c(-1.28, -0.619999999999999,
-1.07, 1.96, 0.15, 4.92, 1.55, 3, -0.459999999999999, -0.56,
1.12, 3.44, -1.48, -1.27, 1.45, 0.609999999999999, -1.59, -1.57,
2.04, 2.03), H1P = c(1.58, -2.15, 1.96, 0.51, 2, 0.37, 1.47,
-1.83, 2.56, -0.62, -1.46, -2.19, -1.77, -1.9, -1.25, -0.73,
-0.57, 1.35, -1.28, -4.14), TRLZ_TRL = c(4.61, 1.49, -2.71, 1.54,
-5.46, 2.18, 3.48, 12.83, 7.51, 7.74, -8.38, -0.729999999999997,
6.11, -19.74, -0.869999999999997, -1.82, -1.57000000000001, 0.609999999999999,
-14.79, -18.65), LDLZ = c(-0.0599999999999987, -0.400000000000002,
-0.289999999999999, -1.2, -0.479999999999997, -0.59, -1.29, -0.0599999999999987,
0.210000000000001, -1.58, 1.97, 0.0800000000000018, -1, 1.95,
1.41, 0.00999999999999801, 0.430000000000003, -0.289999999999999,
0.68, 0.52), HDLZ = c(-0.200000000000001, -0.200000000000001,
-0.0700000000000003, 0, -0.0200000000000014, -0.0199999999999996,
-0.0399999999999991, -0.119999999999999, -0.0900000000000016,
-0.0500000000000007, -0.15, -0.16, -0.640000000000001, 0.42,
0.16, -0.130000000000001, 0.15, 0.41, -0.0300000000000011, 0.18
)), class = "data.frame", row.names = c(NA, -20L))
library(pheatmap)
library(corrplot)
CorMethod <- "spearman"
CorMatrix <- cor(Data_corr, method=CorMethod, use="pairwise.complete.obs")
## 1st Plot
Plot3<-pheatmap(CorMatrix, cluster_cols=T, cluster_rows=T, cutree_rows = 3, angle_col=45, fontsize_col=5, fontsize_row = 7, treeheight_col=0, clustering_method="ward.D2")
#2nd Plot
Plot8 <-corrplot(CorMatrix, method="square", type="lower", order="hclust", hclust.method="ward.D2", tl.pos="ld", tl.cex = 0.5, tl.col="black", tl.srt=45)
You can create a corrplot with the same order given by pheatmap as follows:
#2nd Plot
library(RColorBrewer)
ord <- Plot3$tree_row$order
ReordCorMatrix <- CorMatrix[ord, ord]
Plot8 <-corrplot(ReordCorMatrix, method="square", type="lower", order="original",
hclust.method="ward.D2",
tl.pos="ld", tl.cex = 0.5, tl.col="black", tl.srt=45,
col=colorRampPalette(rev(brewer.pal(n = 7, name="RdYlBu")))(100))

how to export t-tests results from r? can I add a new column to the dataframe containing the resulting p-values?

I managed to run a while loop to run t-tests comparing each row (see code below) & the results print out in the console. Now I am wondering how to export the t-test results from r? Is it possible to add a new column to the original dataframe with the t-test & p-value results?
`a<-1 #row1
while a<11 #number of rows(10)+1
{
b<-t.test(Large_Intestine_WT[a,],Large_Intestine_ALL_MUTANT[a,],paired=FALSE)
print(rownames(Large_Intestine_WT[a,])) print (b) a<-a+1
}`
E.g.
dataframe1:
dput(Large_Intestine_WT[1:10, 1:6])
structure(list(SKCO1_LARGE_INTESTINE = c(50.66, 0, 63.12, 5.57,
9.82, 0, 0.03, 59.69, 54.33, 15.95), SW1463_LARGE_INTESTINE = c(11.99,
0, 158.81, 4.21, 5.61, 0.98, 0.16, 65.32, 69.42, 13.85), C2BBE1_LARGE_INTESTINE = c(90.01,
0.07, 251.76, 3.75, 21.49, 0, 0.02, 135.1, 51.98, 23.82), LS123_LARGE_INTESTINE = c(41.13,
0.02, 83.12, 3.45, 14.64, 0.08, 0.25, 62.92, 14.1, 17.93), LS513_LARGE_INTESTINE = c(15.75,
0, 58.3, 7.28, 12.55, 0.02, 0.01, 46.24, 30.43, 14.89), MDST8_LARGE_INTESTINE = c(16.32,
0, 76.72, 2.69, 16.29, 0.01, 11.71, 86.57, 5.35, 19.58)), row.names = c("ENSG00000000003.10",
"ENSG00000000005.5", "ENSG00000000419.8", "ENSG00000000457.9",
"ENSG00000000460.12", "ENSG00000000938.8", "ENSG00000000971.11",
"ENSG00000001036.9", "ENSG00000001084.6", "ENSG00000001167.10"
), class = "data.frame")
dataframe2:
dput(Large_Intestine_ALL_MUTANT[1:10, 1:6])
structure(list(SW948_LARGE_INTESTINE = c(30.3, 0.86, 117.62,
6.54, 12.54, 0.01, 0.01, 49.54, 44.32, 24.88), CCK81_LARGE_INTESTINE = c(33.6,
0.29, 117.17, 5.41, 15.02, 0.09, 0.06, 112.3, 51.45, 15.18),
RKO_LARGE_INTESTINE = c(11.89, 0.02, 153.59, 1.43, 12.71,
0, 0.13, 56.71, 15.72, 21.12), HCT116_LARGE_INTESTINE = c(26.62,
0, 108.14, 5.4, 23.62, 0, 0.04, 62.43, 52.36, 27.08), T84_LARGE_INTESTINE = c(56.72,
0.31, 75.88, 5.18, 16.03, 0.01, 0.04, 111.02, 41.39, 18.14
), CW2_LARGE_INTESTINE = c(36.58, 0.32, 44.1, 3.44, 7.57,
0.12, 0.1, 37.96, 46.32, 20.36)), row.names = c("ENSG00000000003.10",
"ENSG00000000005.5", "ENSG00000000419.8", "ENSG00000000457.9",
"ENSG00000000460.12", "ENSG00000000938.8", "ENSG00000000971.11",
"ENSG00000001036.9", "ENSG00000001084.6", "ENSG00000001167.10"
), class = "data.frame")
Thanks for your help.

Manipulating color of a filled-contour plot in R

I am very new to R and would like to create a filled contour plot in R to show oxygen concentration (z) changes in water column (y) over time (x). Bellow is how I made the initial plot using filled.contour() function. My questions are:
1- How to change the coloration of the plot? I like it to be between dark blue (high oxygen) to red (low oxygen).
2- As depth appears on Y axis, how can I make it start from 22 m (maximum depth of the lake) instead of 0 m to make plot easier to understand?
Thanks you
mat <- structure(c(9.76, 11.25, 11.15, 8.56, 8.94, 6.51, 8.30, 9.16,
6.75, 6.16, 6.48, 7.18, 7.17, 5.34, 5.03, 9.78, 11.52, 11.22, 7.39,
8.97, 6.48, 8.28, 9.40, 6.69, 6.11, 6.34, 7.06, 6.45, 5.05, 4.51, 9.72,
11.57, 11.21, 8.35, 8.96, 6.47, 8.21, 9.37, 6.60, 6.08, 6.31, 6.90,
5.86, 4.51, 3.56, 10.16, 11.53, 9.75, 8.53, 8.96, 6.44, 8.12, 9.37,
6.48, 6.04, 6.25, 6.81, 5.62, 4.19, 3.21, 10.48, 9.70, 8.78, 7.89,
9.00, 6.43, 8.08, 8.41, 6.33, 6.02, 6.22, 6.68, 5.51, 3.73, 2.88, 8.95,
8.01, 7.48, 7.42, 7.89, 6.41, 7.95, 7.80, 6.13, 5.92, 6.20, 6.52, 4.89,
3.31, 2.27, 7.41, 5.06, 4.10, 4.45, 3.25, 6.40, 7.71, 7.46, 5.97, 5.90,
6.17, 6.15, 4.31, 2.94, 1.72, 5.71, 3.86, 1.27, 1.97, 0.61, 6.38, 5.94,
7.43, 5.89, 5.87, 6.15, 5.81, 3.70, 2.70, 1.26, 5.02, 3.30, 0.80, 0.27,
0.48, 6.34, 4.89, 7.10, 5.87, 5.87, 6.12, 5.52, 3.34, 2.23, 0.58, 3.90,
3.02, 0.43, 0.18, 0.41, 5.92, 2.43, 6.97, 5.91, 5.92, 6.10, 5.01, 3.01,
0.32, 0.18, 3.46, 2.51, 0.24, 0.19, 0.38, 0.51, 0.19, 6.03, 5.92, 5.93,
6.09, 4.79, 1.86, 0.26, 0.10, 2.87, 1.35, 0.18, 0.14, 0.36, 0.23, 0.03,
5.52, 5.90, 5.97, 6.08, 4.46, 0.72, 0.20, 0.09, 1.67, 0.72, 0.12, 0.10,
0.34, 0.14, 0.00, 1.64, 5.72, 5.27, 6.06, 4.05, 0.52, 0.15, 0.07, 0.82,
0.59, 0.09, 0.08, 0.33, 0.13, 0.00, 0.41, 5.66, 5.86, 6.05, 0.90, 0.44,
0.14, 0.06, 0.48, 0.43, 0.07, 0.07, 0.32, 0.11, 0.00, 0.25, 2.47, 5.85,
6.03, 0.49, 0.39, 0.13, 0.05, 0.38, 0.37, 0.07, 0.07, 0.33, 0.10, 0.00,
0.19, 0.14, 5.62, 6.02, 0.35, 0.38, 0.12, 0.05, 0.28, 0.34, 0.07, 0.06,
0.33, 0.09, 0.00, 0.16, 0.08, 4.11, 6.02, 0.28, 0.36, 0.11, 0.04, 0.30,
0.33, 0.09, 0.07, 0.32, 0.09, 0.00, 0.15, 0.06, 0.72, 6.02, 0.24, 0.35,
0.10, 0.03, 0.31, 0.31, 0.11, 0.08, 0.31, 0.08, 0.00, 0.14, 0.04, 0.10,
6.01, 0.21, 0.34, 0.09, 0.02, 0.38, 0.31, 0.09, 0.07, 0.30, 0.08, 0.00,
0.14, 0.03, 0.06, 6.01, 0.22, 0.33, 0.07, 0.02, 0.44, 0.30, 0.07, 0.05,
0.29, 0.07, 0.00, 0.13, 0.02, 0.04, 6.01, 0.26, 0.31, 0.05, 0.02, 0.41,
0.30, 0.04, 0.04, 0.28, 0.06, 0.00, 0.14, 0.01, 0.02, 6.02, 0.27, 0.31,
0.05, 0.02, 0.37, 0.29, 0.04, 0.03, 0.28, 0.06, 0.00, 0.14, 0.00, 0.01,
6.02, 0.26, 0.31, 0.05, 0.02), .Dim = c(15L, 23L), .Dimnames =
list(c("06/25/2015", "07/08/2015","07/22/2015", "08/04/2015",
"08/18/2015", "09/01/2015", "09/15/2015", "09/30/2015","10/14/2015",
"10/26/2015", "11/12/2015", "01/06/2016", "01/20/2016", "02/04/2016",
"02/16/2016"), c("0.5", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
"22")))
dates <- as.Date(dimnames(mat)[[1]], "%m/%d/%Y")
depths <- as.numeric(dimnames(mat)[[2]])
filled.contour(x=dates, y=depths, z=mat, xlab="Date", ylab="Depth")
I couldn't figure out what you were talking about regarding starting at 22, but this will get you part of the way there.
cols = rev(colorRampPalette(c('darkred','red','blue','lightblue'))(24))
filled.contour(x=dates, y=depths, z=mat, xlab="Date", ylab="Depth",
col = cols
)
By the way, if I reversed the coloring then just remove the rev() statement.

Resources