How to perform a polynomial equation and curve? - r

I have this dataframe:
id power hr fr VE VO2 VCO2 PETCO2 percent_VO2 percent_power
1 BM06-PRD-S1 25 119.25 18.25 19.00 0.61675 0.58225 37.6425 48.87084 25.00000
2 BM06-PRD-S1 40 126.00 18.00 20.75 0.71700 0.65950 39.2175 56.81458 40.00000
3 BM06-PRD-S1 55 133.50 20.75 25.00 0.86275 0.82750 41.2150 68.36371 55.00000
4 BM06-PRD-S1 70 147.25 18.25 29.00 0.98575 1.04550 41.7050 78.11014 70.00000
5 BM06-PRD-S1 85 158.50 22.25 39.25 1.13000 1.30525 41.1425 89.54041 85.00000
6 BM06-PRD-S1 100 168.75 27.75 51.00 1.26200 1.61150 38.8925 100.00000 100.00000
7 CB19-PRD-S1 25 98.75 18.50 25.00 0.88350 0.80475 40.7550 36.15715 13.15789
8 CB19-PRD-S1 40 98.25 20.00 25.50 0.94575 0.82900 41.4675 38.70473 21.05263
9 CB19-PRD-S1 55 102.00 19.75 28.50 1.08125 0.95800 42.2775 44.25005 28.94737
10 CB19-PRD-S1 70 107.50 20.50 34.25 1.24400 1.14275 42.6450 50.91058 36.84211
11 CB19-PRD-S1 85 111.00 21.25 35.50 1.30475 1.19925 43.3600 53.39677 44.73684
12 CB19-PRD-S1 100 117.25 21.50 40.25 1.47350 1.42225 44.2650 60.30284 52.63158
13 CB19-PRD-S1 115 123.00 22.75 47.00 1.67900 1.68475 44.6400 68.71291 60.52632
14 CB19-PRD-S1 130 129.50 24.50 52.50 1.79075 1.87950 44.3425 73.28627 68.42105
15 CB19-PRD-S1 145 135.50 25.25 59.50 1.96000 2.13525 44.7300 80.21281 76.31579
16 CB19-PRD-S1 160 145.25 26.75 64.50 2.04050 2.28350 43.8825 83.50726 84.21053
17 CB19-PRD-S1 175 151.25 30.50 83.00 2.34425 2.76050 41.6025 95.93820 92.10526
18 CB19-PRD-S1 190 161.75 33.75 92.25 2.44350 2.96850 40.0400 100.00000 100.00000
19 CC14-PRD-S1 20 102.50 19.00 18.25 0.59250 0.54825 37.7175 49.26211 22.22222
20 CC14-PRD-S1 30 110.25 18.75 19.75 0.66100 0.60325 38.5800 54.95739 33.33333
21 CC14-PRD-S1 40 113.25 18.50 20.75 0.74350 0.66025 39.2950 61.81667 44.44444
22 CC14-PRD-S1 50 122.50 20.00 23.50 0.87875 0.77325 40.5650 73.06173 55.55556
23 CC14-PRD-S1 60 126.25 17.50 26.25 0.94350 0.89375 41.3525 78.44523 66.66667
24 CC14-PRD-S1 70 132.00 16.50 28.00 0.99675 0.98525 42.7575 82.87258 77.77778
25 CC14-PRD-S1 80 145.00 18.50 32.75 1.11425 1.16275 42.5025 92.64186 88.88889
26 CC14-PRD-S1 90 153.50 19.50 37.25 1.20275 1.32700 42.0975 100.00000 100.00000
27 DA24-PRD-S1 25 88.00 18.50 15.75 0.53500 0.45075 37.2200 40.33170 21.73913
28 DA24-PRD-S1 40 93.25 18.50 16.25 0.58450 0.47775 38.3375 44.06332 34.78261
29 DA24-PRD-S1 55 103.75 19.00 20.25 0.76875 0.65450 40.1875 57.95326 47.82609
30 DA24-PRD-S1 70 119.00 20.75 28.00 0.98200 0.95525 41.5175 74.02940 60.86957
31 DA24-PRD-S1 85 133.25 22.75 34.75 1.09975 1.18325 41.4125 82.90614 73.91304
32 DA24-PRD-S1 100 145.00 27.50 45.75 1.25900 1.49700 39.1475 94.91142 86.95652
33 DA24-PRD-S1 115 155.25 36.50 64.75 1.32650 1.72500 33.0275 100.00000 100.00000
I am running a plot using ggplot and ggscatter:
ggplot(dftest, aes(percent_power, PETCO2)) +
geom_point()
ggscatter(dftest, x = "percent_power", y = "PETCO2", add = "reg.line") +
stat_cor(label.x = 20, label.y = 3.8) +
stat_regline_equation(label.x = 20, label.y = 0.5) +
xlab("Percentage of power (%)") +
geom_smooth(method = "lm", colour = "red") +
ylab(expression(paste("PETC", O[2]," (mmHg)")))
I would like to perform a polynomial equation and curve because I am just able to run a linear regression.
Thank you!

I think what you're asking is how to how to fit a polynomial regression line to your data while still using the ggpubr functions to annotate it.
This is possible, but it seems that the in-built regression line can only be either a straight line or a loess model, neither of which is appropriate. However, you can fit a polynomial curve and get the equation and adjusted R-squared on the plot using the method below. In your case I have used a cubic formula, but you should choose your polynomial based on a known model or whatever makes most sense based on what you already know about the relationship between your variables.
You can use ggplot to add the actual line as suggested by #Roland, as long as this uses the same formula as the one you supply to stat_regline_equation
ggscatter(dftest, x = "percent_power", y = "PETCO2") +
stat_regline_equation(label.x = 20, label.y = 0.5,
formula = y ~ poly(x, 3),
aes(label = paste(..eq.label.., ..adj.rr.label.., sep = "~~~~")),) +
geom_smooth(method = "lm", formula = y ~ poly(x, 3)) +
xlab("Percentage of power (%)") +
ylab(expression(paste("PETC", O[2]," (mmHg)")))
Which gives this result:

Related

Interpolation and estimation of data in R

Here are my data showing the cardiopulmonary evolution during an acute exercise with gaz exchange analysis. As we can see, when I report data according the relative percentage of peak power (percent_power), the percentage doesn't correspond between each individuals, because, of course, everyone has a different peak.
Then, I would like to create an interpolation for each individual to estimate specific percentages (e.g. 25%, 50%, 75%) for each individual, and put in a column. Consequently, each variable should be estimated as well and should correspond to the new estimated 25, 50, 75 % of percent_power.
Thank you very much all for your help,
id power training hr fr VE absVO2 VCO2 PETCO2 VES QC IC WCI RVSi RVS VTD FE body_mass percent_absVO2 percent_power relVO2 percent_relVO2 group temps
1 AC12-PRD-C1 25 linear 88.75 22.75 22.75 0.73900 0.66700 39.2925 88.650 8.025 3.975 4.825 1768.75 876.00 143.025 62.050 84.0 49.34068 21.73913 8.797619 49.34068 CHD 1
2 AC12-PRD-C1 40 linear 93.25 23.00 23.75 0.81975 0.71500 39.6200 87.375 8.050 3.975 4.825 1759.50 871.75 141.625 61.725 84.0 54.73210 34.78261 9.758929 54.73210 CHD 1
3 AC12-PRD-C1 55 linear 99.75 22.75 26.75 0.95125 0.85400 41.4100 93.375 9.175 4.550 5.525 1540.50 763.00 150.325 62.100 84.0 63.51193 47.82609 11.324405 63.51193 CHD 1
4 AC12-PRD-C1 70 linear 109.75 23.00 32.50 1.07525 1.04700 42.0150 93.825 10.025 4.925 6.000 1414.25 700.50 145.750 64.375 84.0 71.79102 60.86957 12.800595 71.79102 CHD 1
5 AC12-PRD-C1 85 linear 118.75 22.75 39.50 1.19900 1.25125 41.8425 97.375 11.225 5.575 6.750 1260.75 624.50 148.975 65.325 84.0 80.05341 73.91304 14.273810 80.05341 CHD 1
6 AC12-PRD-C1 100 linear 127.00 26.00 48.25 1.34575 1.51850 41.0950 100.900 12.550 6.225 7.525 1127.75 558.75 154.225 65.475 84.0 89.85144 86.95652 16.020833 89.85144 CHD 1
7 AC12-PRD-C1 115 linear 135.75 28.00 55.75 1.49775 1.76025 40.7275 104.475 13.950 6.875 8.375 1014.00 502.25 157.975 66.250 84.0 100.00000 100.00000 17.830357 100.00000 CHD 1
8 AC12-PRD-C2 25 linear 84.25 20.50 20.75 0.67625 0.59950 38.9575 102.700 8.650 4.275 5.575 1775.00 879.50 216.450 48.350 84.8 40.10378 17.24138 7.974646 40.10378 CHD 2
9 AC12-PRD-C2 40 linear 89.25 20.50 23.25 0.73350 0.66225 38.5500 111.625 9.725 4.800 6.250 1567.75 776.75 217.800 51.825 84.8 43.49889 27.58621 8.649764 43.49889 CHD 2
10 AC12-PRD-C2 55 linear 96.25 22.25 26.75 0.83550 0.77500 38.3350 101.300 9.325 4.625 6.000 1619.75 802.75 202.700 50.350 84.8 49.54781 37.93103 9.852594 49.54781 CHD 2
11 AC12-PRD-C2 70 linear 102.25 21.75 32.50 1.06250 1.01550 39.6525 103.550 10.350 5.125 6.625 1459.00 723.00 194.050 53.675 84.8 63.00964 48.27586 12.529481 63.00964 CHD 2
12 AC12-PRD-C2 85 linear 110.75 22.25 37.75 1.18075 1.19225 40.1300 100.825 10.650 5.275 6.825 1424.00 705.25 194.250 51.900 84.8 70.02224 58.62069 13.923939 70.02224 CHD 2
13 AC12-PRD-C2 100 linear 118.25 23.00 42.75 1.35100 1.40300 41.1500 108.950 12.375 6.100 7.950 1225.50 606.75 197.325 55.175 84.8 80.11861 68.96552 15.931604 80.11861 CHD 2
14 AC12-PRD-C2 115 linear 129.25 24.75 51.25 1.50650 1.65650 40.7575 107.625 13.275 6.550 8.525 1133.50 561.50 201.225 53.525 84.8 89.34025 79.31034 17.765330 89.34025 CHD 2
15 AC12-PRD-C2 130 linear 136.25 26.50 58.75 1.57325 1.83200 39.6750 108.925 14.375 7.125 9.250 1045.75 518.25 196.025 55.675 84.8 93.29874 89.65517 18.552476 93.29874 CHD 2
16 AC12-PRD-C2 145 linear 147.50 29.75 70.00 1.68625 2.07350 38.1600 104.875 15.025 7.450 9.600 1010.75 500.75 185.400 56.825 84.8 100.00000 100.00000 19.885024 100.00000 CHD 2
17 AL13-PRD-C1 25 nonlinear 69.50 16.50 24.00 0.66125 0.58050 31.2275 101.825 7.175 3.500 4.450 2126.50 1037.25 220.850 48.550 85.5 41.36691 19.23077 7.733918 41.36691 CHD 1
18 AL13-PRD-C1 40 nonlinear 73.00 17.50 26.50 0.74850 0.66425 32.1025 107.850 7.775 3.775 4.850 1942.00 947.25 242.825 48.000 85.5 46.82515 30.76923 8.754386 46.82515 CHD 1
19 AL13-PRD-C1 55 nonlinear 83.25 15.50 29.00 0.85500 0.79425 33.6650 110.250 9.075 4.375 5.650 1706.00 832.50 233.500 47.325 85.5 53.48764 42.30769 10.000000 53.48764 CHD 1
20 AL13-PRD-C1 70 nonlinear 93.75 16.00 36.50 0.98450 0.99925 34.5325 114.650 10.425 5.075 6.525 1462.00 713.25 233.075 49.175 85.5 61.58899 53.84615 11.514620 61.58899 CHD 1
21 AL13-PRD-C1 85 nonlinear 104.50 16.00 44.75 1.14950 1.23475 34.4225 120.650 12.150 5.925 7.550 1249.25 609.25 233.575 51.775 85.5 71.91117 65.38462 13.444444 71.91117 CHD 1
22 AL13-PRD-C1 100 nonlinear 114.25 19.25 55.25 1.34650 1.48375 33.1800 115.250 12.775 6.275 7.975 1178.25 574.75 220.375 52.350 85.5 84.23522 76.92308 15.748538 84.23522 CHD 1
23 AL13-PRD-C1 115 nonlinear 125.25 20.75 63.75 1.45100 1.65775 32.6450 117.500 14.100 6.875 8.825 1095.25 534.25 236.575 50.200 85.5 90.77260 88.46154 16.970760 90.77260 CHD 1
24 AL13-PRD-C1 130 nonlinear 136.25 24.75 78.00 1.59850 1.89075 30.9000 119.150 15.575 7.600 9.700 968.25 472.25 231.075 51.600 85.5 100.00000 100.00000 18.695906 100.00000 CHD 1
25 AL13-PRD-C2 25 nonlinear 60.25 15.75 19.00 0.56950 0.46550 32.2575 154.625 9.450 4.700 6.075 1597.75 794.75 348.975 44.850 82.4 30.83378 15.62500 6.911408 30.83378 CHD 2
26 AL13-PRD-C2 40 nonlinear 63.25 14.25 19.50 0.63175 0.52325 33.5700 143.225 9.275 4.625 5.975 1631.75 811.50 326.325 44.575 82.4 34.20411 25.00000 7.666869 34.20411 CHD 2
27 AL13-PRD-C2 55 nonlinear 72.75 15.75 25.00 0.82600 0.69925 34.4600 147.350 10.175 5.075 6.525 1497.25 744.75 312.475 47.950 82.4 44.72117 34.37500 10.024272 44.72117 CHD 2
28 AL13-PRD-C2 70 nonlinear 79.00 15.50 30.75 0.94525 0.86850 34.9675 153.575 11.925 5.925 7.675 1257.00 625.25 271.525 56.625 82.4 51.17759 43.75000 11.471481 51.17759 CHD 2
29 AL13-PRD-C2 85 nonlinear 88.25 16.00 37.50 1.15050 1.08025 35.6175 155.200 13.325 6.625 8.550 1127.00 560.50 282.300 54.975 82.4 62.29020 53.12500 13.962379 62.29020 CHD 2
30 AL13-PRD-C2 100 nonlinear 99.00 16.75 44.75 1.29925 1.31475 35.6475 154.150 14.775 7.325 9.500 1030.75 512.50 285.350 54.500 82.4 70.34380 62.50000 15.767597 70.34380 CHD 2
31 AL13-PRD-C2 115 nonlinear 107.00 18.00 50.00 1.39775 1.45600 36.0325 161.000 16.675 8.300 10.725 898.00 446.50 282.850 57.175 82.4 75.67677 71.87500 16.962985 75.67677 CHD 2
32 AL13-PRD-C2 130 nonlinear 118.50 21.00 61.50 1.55100 1.73675 34.8775 162.300 18.300 9.100 11.750 815.75 405.75 276.700 58.700 82.4 83.97401 81.25000 18.822816 83.97401 CHD 2
33 AL13-PRD-C2 145 nonlinear 128.25 24.25 74.75 1.71275 1.99100 33.3300 161.025 19.925 9.900 12.800 749.50 372.75 267.875 60.175 82.4 92.73146 90.62500 20.785801 92.73146 CHD 2
34 AL13-PRD-C2 160 nonlinear 142.50 29.00 90.50 1.84700 2.21650 30.9325 154.750 20.925 10.425 13.425 715.50 355.75 272.250 57.100 82.4 100.00000 100.00000 22.415049 100.00000 CHD 2

How to average and estimate data in R

I have this data:
id power training hr percent_absVO2 percent_power relVO2 percent_relVO2
1 AC12-PRD-C1 25 linear 88.75 49.34068 21.73913 8.797619 49.34068
2 AC12-PRD-C1 40 linear 93.25 54.73210 34.78261 9.758929 54.73210
3 AC12-PRD-C1 55 linear 99.75 63.51193 47.82609 11.324405 63.51193
4 AC12-PRD-C1 70 linear 109.75 71.79102 60.86957 12.800595 71.79102
5 AC12-PRD-C1 85 linear 118.75 80.05341 73.91304 14.273810 80.05341
6 AC12-PRD-C1 100 linear 127.00 89.85144 86.95652 16.020833 89.85144
7 AC12-PRD-C1 115 linear 135.75 100.00000 100.00000 17.830357 100.00000
8 AC12-PRD-C2 25 linear 84.25 40.10378 17.24138 7.974646 40.10378
9 AC12-PRD-C2 40 linear 89.25 43.49889 27.58621 8.649764 43.49889
10 AC12-PRD-C2 55 linear 96.25 49.54781 37.93103 9.852594 49.54781
11 AC12-PRD-C2 70 linear 102.25 63.00964 48.27586 12.529481 63.00964
12 AC12-PRD-C2 85 linear 110.75 70.02224 58.62069 13.923939 70.02224
13 AC12-PRD-C2 100 linear 118.25 80.11861 68.96552 15.931604 80.11861
14 AC12-PRD-C2 115 linear 129.25 89.34025 79.31034 17.765330 89.34025
15 AC12-PRD-C2 130 linear 136.25 93.29874 89.65517 18.552476 93.29874
16 AC12-PRD-C2 145 linear 147.50 100.00000 100.00000 19.885024 100.00000
17 AL13-PRD-C1 25 nonlinear 69.50 41.36691 19.23077 7.733918 41.36691
18 AL13-PRD-C1 40 nonlinear 73.00 46.82515 30.76923 8.754386 46.82515
19 AL13-PRD-C1 55 nonlinear 83.25 53.48764 42.30769 10.000000 53.48764
20 AL13-PRD-C1 70 nonlinear 93.75 61.58899 53.84615 11.514620 61.58899
21 AL13-PRD-C1 85 nonlinear 104.50 71.91117 65.38462 13.444444 71.91117
22 AL13-PRD-C1 100 nonlinear 114.25 84.23522 76.92308 15.748538 84.23522
23 AL13-PRD-C1 115 nonlinear 125.25 90.77260 88.46154 16.970760 90.77260
24 AL13-PRD-C1 130 nonlinear 136.25 100.00000 100.00000 18.695906 100.00000
25 AL13-PRD-C2 25 nonlinear 60.25 30.83378 15.62500 6.911408 30.83378
26 AL13-PRD-C2 40 nonlinear 63.25 34.20411 25.00000 7.666869 34.20411
27 AL13-PRD-C2 55 nonlinear 72.75 44.72117 34.37500 10.024272 44.72117
28 AL13-PRD-C2 70 nonlinear 79.00 51.17759 43.75000 11.471481 51.17759
29 AL13-PRD-C2 85 nonlinear 88.25 62.29020 53.12500 13.962379 62.29020
30 AL13-PRD-C2 100 nonlinear 99.00 70.34380 62.50000 15.767597 70.34380
31 AL13-PRD-C2 115 nonlinear 107.00 75.67677 71.87500 16.962985 75.67677
32 AL13-PRD-C2 130 nonlinear 118.50 83.97401 81.25000 18.822816 83.97401
33 AL13-PRD-C2 145 nonlinear 128.25 92.73146 90.62500 20.785801 92.73146
34 AL13-PRD-C2 160 nonlinear 142.50 100.00000 100.00000 22.415049 100.00000
As you can see, not everyone has the same percent_power value. I would like to make everyone one the same increment according the percent_power column. For example, I would like to get 25 %, 50 %, 75 %, and 100 %.
That means that the value has to be estimated if I understand well.
Here is an example for only 2 patients of what I would expect. Values in the other column have to be estimated if the exact percent_power doesn't exist.
id power training hr percent_absVO2 percent_power relVO2 percent_relVO2
1 AC12-PRD-C1 25 linear 88.75 49.34068 25.00000 8.797619 49.34068
2 AC12-PRD-C1 55 linear 99.75 63.51193 50.00000 11.324405 63.51193
3 AC12-PRD-C1 85 linear 118.75 80.05341 75.00000 14.273810 80.05341
4 AC12-PRD-C1 115 linear 135.75 100.00000 100.00000 17.830357 100.00000
5 AC12-PRD-C2 40 linear 89.25 43.49889 25.00000 8.649764 43.49889
6 AC12-PRD-C2 70 linear 102.25 63.00964 50.00000 12.529481 63.00964
7 AC12-PRD-C2 115 linear 129.25 89.34025 75.00000 17.765330 89.34025
16 AC12-PRD-C2 145 linear 147.50 100.00000 100.00000 19.885024 100.00000
Thank you for your help !
Your question is not well defined, you want us to predict multiple variables, but you do not tell us how, which is crucial. There are infinitely many ways in which you could "predict" these missing values. For ex. your variables "percent" are limited within 0 and 100, which you have to include in your prediction, or you will get nonsense results.
Having said that, I will do just that and predict all these variables using a simple linear model, which assumes a linear relationship between all your numeric variables and percent_power.
First create a linear model to make your predictions
mod=lm(cbind(power,hr,percent_absVO2,relVO2,percent_relVO2)~percent_power*id+training,data=df)
then create your dataset with all the combinations that you want to predict
tst=setNames(
data.frame(
expand.grid(unique(df[,"id"]),unique(df[,"training"]),seq(25,100,25))
)
,c("id","training","percent_power")
)
and then predict
cbind(
tst,
predict(mod,tst)
)
id training percent_power power hr percent_absVO2 relVO2
1 AC12-PRD-C1 linear 25 28.75 88.08482 49.30491 8.791242
2 AC12-PRD-C2 linear 25 36.25 86.88333 43.57464 8.664828
3 AL13-PRD-C1 linear 25 32.50 70.24554 42.60440 7.965278
4 AL13-PRD-C2 linear 25 40.00 63.65909 36.80735 8.250386
5 AC12-PRD-C1 nonlinear 25 28.75 88.08482 49.30491 8.791242
6 AC12-PRD-C2 nonlinear 25 36.25 86.88333 43.57464 8.664828
7 AL13-PRD-C1 nonlinear 25 32.50 70.24554 42.60440 7.965278
8 AL13-PRD-C2 nonlinear 25 40.00 63.65909 36.80735 8.250386
9 AC12-PRD-C1 linear 50 57.50 103.65774 65.64847 11.705357
10 AC12-PRD-C2 linear 50 72.50 106.05556 63.13669 12.554745
11 AL13-PRD-C1 linear 50 65.00 91.71230 61.50428 11.498782
12 AL13-PRD-C2 linear 50 80.00 88.20455 57.97911 12.996047
13 AC12-PRD-C1 nonlinear 50 57.50 103.65774 65.64847 11.705357
14 AC12-PRD-C2 nonlinear 50 72.50 106.05556 63.13669 12.554745
15 AL13-PRD-C1 nonlinear 50 65.00 91.71230 61.50428 11.498782
16 AL13-PRD-C2 nonlinear 50 80.00 88.20455 57.97911 12.996047
17 AC12-PRD-C1 linear 75 86.25 119.23065 81.99203 14.619473
18 AC12-PRD-C2 linear 75 108.75 125.22778 82.69873 16.444662
19 AL13-PRD-C1 linear 75 97.50 113.17907 80.40415 15.032285
20 AL13-PRD-C2 linear 75 120.00 112.75000 79.15088 17.741707
21 AC12-PRD-C1 nonlinear 75 86.25 119.23065 81.99203 14.619473
22 AC12-PRD-C2 nonlinear 75 108.75 125.22778 82.69873 16.444662
23 AL13-PRD-C1 nonlinear 75 97.50 113.17907 80.40415 15.032285
24 AL13-PRD-C2 nonlinear 75 120.00 112.75000 79.15088 17.741707
25 AC12-PRD-C1 linear 100 115.00 134.80357 98.33560 17.533588
26 AC12-PRD-C2 linear 100 145.00 144.40000 102.26077 20.334578
27 AL13-PRD-C1 linear 100 130.00 134.64583 99.30403 18.565789
28 AL13-PRD-C2 linear 100 160.00 137.29545 100.32264 22.487368
29 AC12-PRD-C1 nonlinear 100 115.00 134.80357 98.33560 17.533588
30 AC12-PRD-C2 nonlinear 100 145.00 144.40000 102.26077 20.334578
31 AL13-PRD-C1 nonlinear 100 130.00 134.64583 99.30403 18.565789
32 AL13-PRD-C2 nonlinear 100 160.00 137.29545 100.32264 22.487368
percent_relVO2
1 49.30491
2 43.57464
3 42.60440
4 36.80735
5 49.30491
6 43.57464
7 42.60440
8 36.80735
9 65.64847
10 63.13669
11 61.50428
12 57.97911
13 65.64847
14 63.13669
15 61.50428
16 57.97911
17 81.99203
18 82.69873
19 80.40415
20 79.15088
21 81.99203
22 82.69873
23 80.40415
24 79.15088
25 98.33560
26 102.26077
27 99.30403
28 100.32264
29 98.33560
30 102.26077
31 99.30403
32 100.32264
Note how the percents have gone over 100, which was to be expected, given our model.
When I got you correct, that is what you want.
library(dplyr)
df <- structure(list(id = c("AC12-PRD-C1", "AC12-PRD-C1", "AC12-PRD-C1",
"AC12-PRD-C1", "AC12-PRD-C1", "AC12-PRD-C1", "AC12-PRD-C1", "AC12-PRD-C2",
"AC12-PRD-C2", "AC12-PRD-C2", "AC12-PRD-C2", "AC12-PRD-C2", "AC12-PRD-C2",
"AC12-PRD-C2", "AC12-PRD-C2", "AC12-PRD-C2", "AL13-PRD-C1", "AL13-PRD-C1",
"AL13-PRD-C1", "AL13-PRD-C1", "AL13-PRD-C1", "AL13-PRD-C1", "AL13-PRD-C1",
"AL13-PRD-C1", "AL13-PRD-C2", "AL13-PRD-C2", "AL13-PRD-C2", "AL13-PRD-C2",
"AL13-PRD-C2", "AL13-PRD-C2", "AL13-PRD-C2", "AL13-PRD-C2", "AL13-PRD-C2"
), power = c(25, 40, 55, 70, 85, 100, 115, 25, 40, 55, 70, 85,
100, 115, 130, 145, 25, 40, 55, 70, 85, 100, 115, 130, 25, 40,
55, 70, 85, 100, 115, 130, 145), training = c("linear", "linear",
"linear", "linear", "linear", "linear", "linear", "linear", "linear",
"linear", "linear", "linear", "linear", "linear", "linear", "linear",
"nonlinear", "nonlinear", "nonlinear", "nonlinear", "nonlinear",
"nonlinear", "nonlinear", "nonlinear", "nonlinear", "nonlinear",
"nonlinear", "nonlinear", "nonlinear", "nonlinear", "nonlinear",
"nonlinear", "nonlinear"), hr = c(88.75, 93.25, 99.75, 109.75,
118.75, 127, 135.75, 84.25, 89.25, 96.25, 102.25, 110.75, 118.25,
129.25, 136.25, 147.5, 69.5, 73, 83.25, 93.75, 104.5, 114.25,
125.25, 136.25, 60.25, 63.25, 72.75, 79, 88.25, 99, 107, 118.5,
128.25), percent_absVO2 = c(49.34068, 54.7321, 63.51193, 71.79102,
80.05341, 89.85144, 100, 40.10378, 43.49889, 49.54781, 63.00964,
70.02224, 80.11861, 89.34025, 93.29874, 100, 41.36691, 46.82515,
53.48764, 61.58899, 71.91117, 84.23522, 90.7726, 100, 30.83378,
34.20411, 44.72117, 51.17759, 62.2902, 70.3438, 75.67677, 83.97401,
92.73146), percent_power = c(21.73913, 34.78261, 47.82609, 60.86957,
73.91304, 86.95652, 100, 17.24138, 27.58621, 37.93103, 48.27586,
58.62069, 68.96552, 79.31034, 89.65517, 100, 19.23077, 30.76923,
42.30769, 53.84615, 65.38462, 76.92308, 88.46154, 100, 15.625,
25, 34.375, 43.75, 53.125, 62.5, 71.875, 81.25, 90.625), relVO2 = c(8.797619,
9.758929, 11.324405, 12.800595, 14.273810, 16.020833,
17.830357, 7.974646, 8.649764, 9.852594, 12.529481,
13.923939, 15.931604, 17.765330, 18.552476, 19.885024,
7.733918, 8.754386, 10.000000, 11.514620, 13.444444,
15.748538, 16.970760, 18.695906, 6.911408, 7.666869,
10.024272, 11.471481, 13.962379, 15.767597, 16.962985,
18.822816, 20.785801), percent_relVO2 = c(49.34068, 54.7321,
63.51193, 71.79102, 80.05341, 89.85144, 100, 40.10378, 43.49889,
49.54781, 63.00964, 70.02224, 80.11861, 89.34025, 93.29874, 100,
41.36691, 46.82515, 53.48764, 61.58899, 71.91117, 84.23522, 90.7726,
100, 30.83378, 34.20411, 44.72117, 51.17759, 62.2902, 70.3438,
75.67677, 83.97401, 92.73146)), row.names = c(NA, -33L), class = c("tbl_df",
"tbl", "data.frame"))
df %>% dplyr::mutate(., percent_power = dplyr::case_when(
percent_absVO2 < 25 ~ 0,
percent_absVO2 < 50 ~ 25,
percent_absVO2 < 75 ~ 50,
percent_absVO2 < 100 ~ 75,
TRUE ~ 100
))

Different output when printed vs saved as a data frame

I'm trying to create a summary data frame which has averages, mins and maxes (see original post here). I can print the output I want, but whenever I try to save it to a data frame, I only get the averages, not the mins and maxes. I've tried updating R and tidyr, and I can't think of anything else that would cause this. I've tried using as.data.frame() and that doesn't help.
#example df
df <- read.table(header=TRUE, text="shop tables chairs beds
jim-1 2 63 31
jim-2a 10 4 16
jim-2b 32 34 43
jen-1 32 90 32
jen-2 73 91 6
jen-3 35 85 65
sam-a 72 57 72
sam-b 18 48 11
sam-c 34 49 79
paul-1 43 49 23
paul-2 76 20 23
paul-2a 34 20 8")
#create a grouping to allow me to average out group values
shop_group = sub("-.*", "", df$shop)
#print a summary table (works fine)
aggregate(df[,2:4], list(shop_group),
FUN = function(x) summary(x)[c(4,1,6)])
#generate a summary data frame (doesn't work, only gives me the averages, not the mins and maxes)
summ_df= aggregate(df[,2:4], list(shop_group),
FUN = function(x) summary(x)[c(4,1,6)])
it works fine..
> aggregate(df[,2:4], list(shop_group),
+ FUN = function(x) summary(x)[c(4,1,6)])
Group.1 tables.Mean tables.Min. tables.Max. chairs.Mean chairs.Min. chairs.Max.
1 jen 46.67 32.00 73.00 88.67 85.00 91.00
2 jim 14.67 2.00 32.00 33.67 4.00 63.00
3 paul 51.00 34.00 76.00 29.67 20.00 49.00
4 sam 41.33 18.00 72.00 51.33 48.00 57.00
beds.Mean beds.Min. beds.Max.
1 34.33 6.00 65.00
2 30.00 16.00 43.00
3 18.00 8.00 23.00
4 54.00 11.00 79.00
> summ_df= aggregate(df[,2:4], list(shop_group),
+ FUN = function(x) summary(x)[c(4,1,6)])
> summ_df
Group.1 tables.Mean tables.Min. tables.Max. chairs.Mean chairs.Min. chairs.Max.
1 jen 46.67 32.00 73.00 88.67 85.00 91.00
2 jim 14.67 2.00 32.00 33.67 4.00 63.00
3 paul 51.00 34.00 76.00 29.67 20.00 49.00
4 sam 41.33 18.00 72.00 51.33 48.00 57.00
beds.Mean beds.Min. beds.Max.
1 34.33 6.00 65.00
2 30.00 16.00 43.00
3 18.00 8.00 23.00
4 54.00 11.00 79.00
>

PerformanceAnalytics + data.frame: Formatting issue when using package functions

I'm trying to pass a data.frame with dates in the rownames into a PerformanceAnalytics (package) function.
My data is below, I get the following error message when trying to run the code
> charts.PerformanceSummary(x$Return)
Error in checkData(R) :
The data cannot be converted into a time series. If you are trying to pass in names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.
How can this be solved? I thought that PerformanceAnalytics would recognize my rows as dates automatically.
My data
> str(x)
'data.frame': 201 obs. of 7 variables:
$ Open : num 69 67.8 64 67.2 68 ...
$ High : num 70 69 67.8 68.8 70.8 ...
$ Low : num 67 66 63.2 64.5 67.5 ...
$ Close : num 68.5 66 67.2 68 68.5 ...
$ Volume : num 2163200 2456100 2783000 2200300 2418000 ...
$ Adj.Close: num 55 53 54 54.6 55 ...
$ Return : num 0 0 0 0 0 0 0 0 0 0 ...
> x
Open High Low Close Volume Adj.Close Return
2008-11-28 69.00 70.00 67.00 68.50 2163200 54.9989 0.000000000
2008-12-01 67.75 69.00 66.00 66.00 2456100 52.9916 0.000000000
2008-12-02 64.00 67.75 63.25 67.25 2783000 53.9953 0.000000000
2008-12-03 67.25 68.75 64.50 68.00 2200300 54.5974 0.000000000
2008-12-04 68.00 70.75 67.50 68.50 2418000 54.9989 0.000000000
2008-12-05 68.00 68.75 63.50 64.50 2162200 51.7873 0.000000000
2008-12-08 67.25 73.50 67.25 72.00 4363100 57.8090 0.000000000
2008-12-09 71.25 76.25 70.00 74.50 4266500 59.8163 0.000000000
2008-12-10 73.75 78.00 73.00 77.50 3395900 62.2250 0.000000000
2008-12-11 76.75 76.75 74.25 76.00 2474600 61.0207 0.000000000
2008-12-12 74.00 74.75 72.25 74.50 3141800 59.8163 0.000000000
2008-12-15 70.00 74.50 67.25 67.25 7125600 53.9953 0.000000000
2008-12-16 66.25 69.50 66.00 69.50 3063200 55.8018 0.000000000
2008-12-17 69.25 70.00 67.00 70.00 2543400 56.2032 0.000000000
2008-12-18 69.50 73.50 69.25 72.25 4366000 58.0098 0.000000000
2008-12-19 70.50 72.00 69.75 71.75 2346600 57.6083 0.000000000
2008-12-22 70.50 71.00 68.25 69.75 3057600 56.0025 0.000000000
2008-12-23 68.75 69.50 67.50 69.25 2327700 55.6011 0.000000000
2008-12-24 69.25 69.25 69.25 69.25 0 55.6011 0.000000000
2008-12-25 69.25 69.25 69.25 69.25 0 55.6011 0.000000000
2008-12-26 69.25 69.25 69.25 69.25 0 55.6011 0.000000000
2008-12-29 69.00 69.75 67.50 68.00 1187600 54.5974 0.000000000
2008-12-30 67.75 68.50 66.50 66.75 1888700 53.5938 0.000000000
2008-12-31 66.75 66.75 66.75 66.75 0 53.5938 0.000000000
2009-01-01 66.75 66.75 66.75 66.75 0 53.5938 0.000000000
2009-01-02 67.75 72.75 67.00 72.25 1887500 58.0098 0.000000000
2009-01-05 73.75 75.50 73.00 74.25 2020400 59.6156 0.000000000
2009-01-06 74.25 74.25 74.25 74.25 0 59.6156 0.000000000
2009-01-07 73.75 75.00 73.50 74.50 2962600 59.8163 0.000000000
2009-01-08 73.00 74.25 71.00 72.25 2239100 58.0098 0.000000000
2009-01-09 72.00 72.25 70.25 71.25 1983600 57.2069 0.000000000
2009-01-12 69.50 71.00 69.00 69.75 1487000 56.0025 0.000000000
2009-01-13 68.75 69.75 67.75 68.75 2859100 55.1996 0.000000000
2009-01-14 69.75 69.75 63.25 64.25 2586900 51.5865 0.000000000
2009-01-15 64.00 66.50 60.75 63.00 4090800 50.5829 0.000000000
2009-01-16 65.00 66.00 64.00 64.75 2455600 51.9880 0.000000000
2009-01-19 66.00 66.00 64.75 65.50 2839200 52.5902 0.000000000
2009-01-20 66.00 66.75 62.25 62.75 2787600 50.3822 0.000000000
2009-01-21 62.00 67.50 61.75 65.25 5090700 52.3894 0.000000000
2009-01-22 67.00 69.00 65.50 65.75 2994600 52.7909 0.000000000
2009-01-23 64.75 66.00 61.25 62.00 4463400 49.7800 0.000000000
2009-01-26 62.00 65.75 61.50 65.75 3698700 52.7909 0.000000000
2009-01-27 66.00 67.25 64.75 65.75 3445900 52.7909 0.000000000
2009-01-28 66.25 69.00 66.00 67.00 2766400 53.7945 0.000000000
2009-01-29 67.00 67.25 62.25 62.75 2090700 50.3822 0.000000000
2009-01-30 63.00 63.75 59.00 60.50 6437600 48.5757 0.000000000
2009-02-02 60.25 62.00 58.75 60.25 3267600 48.3749 0.000000000
2009-02-03 60.75 62.50 59.25 62.50 3148000 50.1815 0.000000000
2009-02-04 62.75 69.50 61.75 68.25 6579500 54.7982 0.000000000
2009-02-05 66.00 69.50 66.00 69.00 6733600 55.4003 0.000000000
2009-02-06 69.00 71.00 68.25 70.00 5031600 56.2032 0.000000000
2009-02-09 69.50 72.00 68.75 71.75 3794200 57.6083 0.000000000
2009-02-10 70.25 72.00 69.75 70.75 2991500 56.8054 0.000000000
2009-02-11 68.75 70.75 68.25 68.50 2566400 54.9989 0.000000000
2009-02-12 67.75 70.25 66.75 70.00 3083800 56.2032 0.000000000
2009-02-13 70.25 71.50 68.00 69.25 2227000 55.6011 0.000000000
2009-02-16 68.50 69.25 67.25 68.75 1087100 55.1996 0.000000000
2009-02-17 67.50 67.50 65.00 65.50 1630200 52.5902 0.000000000
2009-02-18 65.00 65.50 62.50 65.00 40400 52.1887 0.000000000
2009-02-19 65.00 68.00 64.50 66.50 2701100 53.3931 0.000000000
2009-02-20 64.25 65.00 60.75 61.25 5534300 49.1778 0.000000000
2009-02-23 62.00 62.50 58.50 58.75 2524300 47.1706 0.000000000
2009-02-24 58.75 60.25 56.75 58.00 5117600 46.5684 0.000000000
2009-02-25 59.50 61.75 59.25 60.25 4418400 48.3749 0.000000000
2009-02-26 61.50 64.50 60.50 63.50 6482600 50.9844 0.000000000
2009-02-27 61.50 62.75 60.00 62.00 2834500 49.7800 0.000000000
2009-03-02 61.25 61.25 58.00 59.50 3087100 47.7727 0.000000000
2009-03-03 60.00 60.75 57.75 58.50 2606300 46.9698 0.000000000
2009-03-04 60.00 60.00 58.00 59.75 4554800 47.9735 0.000000000
2009-03-05 59.00 59.75 57.25 58.00 2777600 46.5684 0.000000000
2009-03-06 56.50 58.75 56.50 57.50 3338300 46.1669 0.000000000
2009-03-09 58.50 58.75 57.00 58.00 3708500 46.5684 0.000000000
2009-03-10 58.00 61.00 57.50 60.25 5163300 48.3749 0.000000000
2009-03-11 60.00 62.75 59.00 61.50 3668700 49.3786 0.000000000
2009-03-12 61.50 61.75 58.50 61.25 3404600 49.1778 0.000000000
2009-03-13 63.00 64.00 60.75 62.00 4048500 49.7800 0.000000000
2009-03-16 63.00 64.50 62.25 62.50 2945300 50.1815 0.000000000
2009-03-17 62.75 62.75 60.50 60.75 2369800 48.7764 0.000000000
2009-03-18 62.25 62.50 59.25 60.25 1968600 48.3749 0.000000000
2009-03-19 61.00 64.75 60.25 64.25 3730400 51.5865 0.000000000
2009-03-20 63.75 65.00 63.50 63.50 3186200 50.9844 0.000000000
2009-03-23 64.75 65.50 63.00 65.50 2738400 52.5902 0.000000000
2009-03-24 65.50 66.00 63.00 64.50 2994200 51.7873 0.000000000
2009-03-25 63.75 69.00 62.75 67.75 4342500 54.3967 0.000000000
2009-03-26 67.00 69.25 66.75 68.00 8300 54.5974 0.000000000
2009-03-27 67.75 69.50 66.75 68.50 3407200 54.9989 0.000000000
2009-03-30 66.75 67.00 63.75 64.50 3123200 51.7873 0.000000000
2009-03-31 64.00 66.25 63.00 64.50 3261600 51.7873 0.000000000
2009-04-01 64.50 64.75 60.75 64.25 6808900 51.5865 0.000000000
2009-04-02 67.00 73.25 66.75 72.75 8180500 58.4112 0.000000000
2009-04-03 72.00 76.50 71.25 75.75 5628500 60.8199 0.000000000
2009-04-06 76.00 76.75 74.25 75.00 3238900 60.2178 0.000000000
2009-04-07 75.75 76.00 72.25 73.75 3765800 59.2141 0.000000000
2009-04-08 72.00 75.75 71.50 75.50 3177100 60.6192 0.000000000
2009-04-09 75.50 76.00 74.25 76.00 887600 61.0207 0.000000000
2009-04-10 76.00 76.00 76.00 76.00 0 61.0207 0.000000000
2009-04-13 76.00 76.00 76.00 76.00 0 61.0207 0.000000000
2009-04-14 78.00 80.75 77.00 79.00 3923200 63.4294 0.000000000
2009-04-15 78.00 79.00 75.75 76.00 2425700 61.0207 0.000000000
2009-04-16 77.50 79.50 76.00 79.25 3331200 63.6301 0.000000000
2009-04-17 79.25 83.00 79.00 81.25 4058600 65.2359 0.000000000
2009-04-20 80.00 80.50 75.75 76.75 3682800 61.6228 0.000000000
2009-04-21 76.25 76.75 73.25 75.75 3293500 60.8199 0.000000000
2009-04-22 84.00 91.50 82.00 88.00 10525000 70.6555 0.000000000
2009-04-23 89.75 92.75 88.75 90.25 5664200 72.4620 0.000000000
2009-04-24 90.75 94.75 89.50 93.75 3982600 75.2722 0.000000000
2009-04-27 92.25 100.25 91.75 97.50 4702300 78.2831 0.000000000
2009-04-28 95.75 96.00 92.25 95.00 6479800 76.2758 0.000000000
2009-04-29 94.50 97.50 92.75 95.00 2565300 76.2758 0.000000000
2009-04-30 95.00 96.75 92.00 92.00 2480500 73.8671 0.000000000
2009-05-01 92.00 92.00 92.00 92.00 0 73.8671 0.000000000
2009-05-04 95.75 100.00 94.00 100.00 3653000 80.2903 0.086956169
2009-05-05 100.00 101.75 97.75 97.75 3400 78.4838 -0.022499605
2009-05-06 98.00 104.00 96.50 102.75 4010500 82.4983 0.051150683
2009-05-07 104.25 105.00 98.50 99.75 5145800 80.0896 -0.029196965
2009-05-08 101.25 103.25 99.25 100.25 2782200 80.4911 0.005013135
2009-05-11 100.25 100.75 93.50 97.25 3925100 78.0824 -0.029925048
2009-05-12 96.50 99.00 94.00 94.50 2770700 75.8744 -0.028277819
2009-05-13 94.75 95.50 88.25 89.00 4618500 71.4584 -0.058201449
2009-05-14 89.00 92.75 87.50 91.75 2712300 73.6664 0.030899097
2009-05-15 94.00 94.00 91.00 92.50 1675400 74.2686 0.008174690
2009-05-18 91.25 97.75 91.00 97.00 3331500 77.8816 0.048647746
2009-05-19 99.25 103.75 98.75 99.50 5343000 79.8889 0.025773739
2009-05-20 100.25 101.00 98.50 100.25 1169700 80.4911 0.007537968
2009-05-21 100.25 100.25 100.25 100.25 0 80.4911 0.000000000
2009-05-22 98.00 99.50 95.75 96.75 2095400 77.6809 -0.034913177
2009-05-25 96.50 97.75 95.50 96.00 1016600 77.0787 -0.007752227
2009-05-26 94.75 96.75 90.75 96.00 3642600 77.0787 0.000000000
2009-05-27 97.50 98.00 96.75 97.50 3729400 78.2831 0.015625588
2009-05-28 96.25 99.00 96.00 98.00 3575300 78.6845 0.005127543
2009-05-29 99.25 100.00 95.25 95.50 3396900 76.6773 -0.025509471
2009-06-01 97.00 100.75 97.00 100.75 1963400 80.8925 0.054973245
2009-06-02 98.00 99.25 95.75 98.00 3547300 78.6845 -0.027295485
2009-06-03 98.75 98.75 95.75 96.75 1865100 77.6809 -0.012754736
2009-06-04 97.25 98.00 96.75 98.00 2496300 78.6845 0.012919521
2009-06-05 98.75 102.25 98.50 100.25 3226800 80.4911 0.022960049
2009-06-08 100.25 101.00 97.25 99.50 2333200 79.8889 -0.007481572
2009-06-09 100.25 103.00 100.25 102.25 4285900 82.0969 0.027638383
2009-06-10 103.25 105.00 102.50 103.50 3933000 83.1005 0.012224579
2009-06-11 105.75 109.00 105.50 108.00 7400700 86.7136 0.043478679
2009-06-12 108.50 110.50 107.50 109.25 3325200 87.7172 0.011573732
2009-06-15 108.25 108.25 103.50 104.00 1848400 83.5019 -0.048055569
2009-06-16 103.75 105.00 102.75 102.75 21000 82.4983 -0.012018888
2009-06-17 101.50 102.25 97.50 99.00 4273700 79.4874 -0.036496510
2009-06-18 99.75 100.75 98.25 100.00 1910400 80.2903 0.010100972
2009-06-19 100.00 100.00 100.00 100.00 0 80.2903 0.000000000
2009-06-22 99.00 99.75 94.00 94.50 3168000 75.8744 0.000000000
2009-06-23 94.50 98.75 94.25 97.50 2356000 78.2831 0.000000000
2009-06-24 99.00 102.50 98.00 102.00 2184800 81.8961 0.046153001
2009-06-25 101.50 103.00 100.00 103.00 1950300 82.6990 0.009803886
2009-06-26 105.00 107.25 103.75 104.50 2649900 83.9034 0.014563659
2009-06-29 105.00 109.25 104.50 108.75 2420700 87.3157 0.040669389
2009-06-30 109.75 110.50 107.25 107.75 2999500 86.5128 -0.009195368
2009-07-01 108.50 111.50 108.50 111.00 2335800 89.1223 0.030163167
2009-07-02 110.50 111.00 106.75 108.00 3151600 86.7136 -0.027026906
2009-07-03 108.00 108.50 106.00 107.00 809300 85.9107 -0.009259217
2009-07-06 106.75 107.50 104.75 106.25 2030400 85.3085 -0.007009604
2009-07-07 107.00 109.50 105.75 107.50 2644100 86.3121 0.011764361
2009-07-08 107.00 108.75 106.25 106.75 3189000 85.7099 -0.006977006
2009-07-09 108.50 110.25 108.50 109.00 3032300 87.5165 0.021078078
2009-07-10 109.25 111.50 108.25 109.25 2637000 87.7172 0.002293282
2009-07-13 108.75 111.25 106.75 110.00 1901300 88.3194 0.006865244
2009-07-14 111.25 112.00 109.25 110.75 2061800 88.9215 0.006817302
2009-07-15 112.00 116.75 111.25 116.75 4185600 93.7390 0.054176999
2009-07-16 125.00 130.50 124.25 127.50 9537100 102.3702 0.092076937
2009-07-17 129.00 131.00 126.25 129.75 2709700 104.1767 0.017646737
2009-07-20 130.00 132.00 129.50 131.00 1762400 105.1803 0.009633632
2009-07-21 131.50 132.25 126.50 127.25 3644800 102.1695 -0.028625132
2009-07-22 127.50 132.25 127.50 130.75 4810100 104.9796 0.027504294
2009-07-23 131.25 131.25 126.75 130.00 3299000 104.3774 -0.005736353
2009-07-24 129.00 133.50 129.00 133.00 52200 106.7861 0.023076835
2009-07-27 134.50 134.75 130.00 132.00 2038900 105.9832 -0.007518769
2009-07-28 132.00 132.75 128.75 130.00 2058000 104.3774 -0.015151458
2009-07-29 130.00 131.50 127.00 128.25 2245700 102.9724 -0.013460768
2009-07-30 130.25 134.25 129.25 134.00 3305500 107.5890 0.044833373
2009-07-31 133.25 138.25 133.00 135.00 3155600 108.3920 0.007463588
2009-08-03 135.00 138.00 134.00 137.25 1916800 110.1985 0.016666359
2009-08-04 136.75 136.75 131.00 132.50 2139700 106.3847 -0.034608457
2009-08-05 132.00 136.00 132.00 134.50 2780800 107.9905 0.015094276
2009-08-06 135.50 139.25 135.00 136.00 2847500 109.1949 0.011152833
2009-08-07 135.25 141.00 134.50 140.00 3169600 112.4065 0.029411630
2009-08-10 139.75 139.75 137.75 139.00 1840700 111.6036 -0.007142825
2009-08-11 138.75 140.00 135.00 135.25 2143500 108.5927 -0.026978520
2009-08-12 134.75 138.50 134.50 138.50 2653600 111.2021 0.024029240
2009-08-13 139.00 149.75 138.75 143.75 8064400 115.4174 0.037906658
2009-08-14 144.25 148.50 141.50 142.75 40600 114.6145 -0.006956490
2009-08-17 141.25 142.00 137.25 137.50 2820500 110.3992 -0.036778069
2009-08-18 138.00 139.00 134.50 137.25 3389500 110.1985 -0.001817948
2009-08-19 136.00 139.75 134.25 138.50 3337300 111.2021 0.009107202
2009-08-20 141.00 148.25 141.00 147.50 5540400 118.4282 0.064981686
2009-08-21 147.50 154.00 147.25 153.50 5896100 123.2457 0.040678656
2009-08-24 155.50 157.00 150.00 154.00 3100300 123.6471 0.003256909
2009-08-25 151.00 151.00 148.75 149.75 3166000 120.2348 -0.027597089
2009-08-26 148.80 149.80 142.40 146.30 4975000 117.4648 -0.023038255
2009-08-27 144.70 146.30 142.70 145.30 2433000 116.6619 -0.006835239
2009-08-28 147.10 153.50 146.40 152.50 4590400 122.4428 0.049552596
2009-08-31 151.50 152.75 146.80 147.50 2984800 118.4282 -0.032787555
2009-09-01 148.60 148.90 145.10 145.30 3402800 116.6619 -0.014914522
2009-09-02 144.00 146.30 140.50 145.60 3403400 116.9027 0.002064084
2009-09-03 146.10 148.40 144.90 147.00 2835000 118.0268 0.009615689
2009-09-04 149.40 153.25 149.10 153.25 3937700 123.0449 0.042516615
x$Return is a vector, not a data.frame and therefore does not have rownames. Use x["Return"] or as.xts(d)[,"Return"] instead.

R format decimal places of result in table

I would like to round the output to one decimal place in mean table
aov_two <- aov(Mass ~ Distance + Colony + Distance:Colony, data = seed_ant)
summary(aov_two)
B <- model.tables(aov_two, "means")
model table of mean result is,
B
Tables of means
Grand mean
54.2461
Distance
0 5 10
56.53 53.19 52.25
rep 221.00 217.00 139.00
Colony
101 2 23 25 28 3 4 X
64.91 41.84 51.44 60.55 50.83 45.32 54.25 60.85
rep 82.00 71.00 52.00 76.00 59.00 77.00 75.00 85.00
Distance:Colony
Colony
Distance 101 2 23 25 28 3 4 X
0 61.79 41.04 51.97 74.52 53.45 41.33 53.26 72.04
rep 29.00 24.00 29.00 29.00 29.00 27.00 27.00 27.00
5 70.33 40.45 52.61 55.20 49.47 47.75 54.18 57.15
rep 27.00 29.00 23.00 25.00 30.00 28.00 28.00 27.00
10 62.23 44.50 48.05 46.59 55.30 53.42
rep 26.00 18.00 0.00 22.00 0.00 22.00 20.00 31.00
how can I round all those numbers to one decimal place?
It's rather difficult to read the data.frames in your question, but try:
df <- round(df, 1)

Resources