我在我的Rmd文件中嵌入了一些代码来对一组数据执行t.test。但奇怪的是,每次点击“编织HTML”时,我都会在HTML文件中看到不同的p值输出。但如果我在控制台中运行代码,则不会发生同样的情况。有人可以帮助理解为什么它必须发生以及如何避免它? 下面是那段代码:
```{r, echo=TRUE}
tresults <- matrix(nrow = 4, ncol = 3)
for (i in 1:4){
options(scipen = 999) #Force are to not use exponential notation
tresult <- t.test(years[[i]][,11], years[[i]][,21], var.equal = T, alternative = "greater")
tresults[i,] <- as.numeric(c(tresult[[1]], tresult[[2]], tresult[[3]]))}
tresults <- format(round(tresults, 3), nsmall = 3) #round off to 3 decimal points
tresults <- cbind(c("2004-05", "2005-06", "2006-07", "2007-08"), tresults)
colnames(tresults) <- c("years", "t", "df", "p-value")
print(tresults)
```
编辑: 'years'基本上是一个包含与每年相对应的数据框的列表 -
#List the datasets
years <- list(prod_data_0405, prod_data_0506, prod_data_0607, prod_data_0708)
我已创建此列表的示例,将其转换为数据框,以便使用以下命令进行共享:
years_sample <- as.data.frame(do.call(rbind, years_sample))
并将其保存在this link上。请用它来测试代码并告诉我。
编辑2 这是我使用dput(年)
创建的示例list(structure(list(ID = c(1, 2, 3, 4, 5, 6), Area..acres. = c(4,
1, 2, 3, 1, 1), Cotton = c(2, 2.5, 2, 5, 3, 0), Pigeon.pea = c(0.33,
NaN, 0.5, 0.21, NaN, 0), Soyabean = c(4, NaN, NaN, 6, NaN, NaN
), Sorghum = c("", "", "", "6", "", ""), Other = c(NaN, NaN,
NaN, 1.6, NaN, NaN), Total.yield..Quintal. = c(6.33, 2.5, 5,
23.56, 3, 0), Gross.Income..Rs. = c(4695, 4750, 4612.5, 11531.67,
5700, 3800), Total.Expenditure..Rs. = c(1955, 1700, 2237, 3296.67,
1520, 2900), Net.Income..Rs. = c(2740, 3050, 2366.5, 235, 4180,
0), Area..acres..1 = c(1, 8, 2, 3, 5, 5), Cotton.1 = c("", "4.6",
"5", "", "2.33", "0"), Pigeon.pea.1 = c(NaN, 0.4, 0.5, 0.33,
0.2, 0), Soyabean.1 = c(NaN, 2, NaN, 0.23, 3, NaN), Sorghum.1 = c(4,
11, NaN, NaN, 4.5, 0), Other.1 = c(NaN, NaN, NaN, NaN, NaN, NaN
), Total.yield..Quintal..1 = c(4, 40, 11, 1.7, 15.1, 0), Gross.Income..Rs..1 = c(3200,
7603.12, 7612.5, 1013.33, 4705, 5980), Total.Expenditure..Rs..1 = c(1950,
3042.7, 2850, 1193.33, 2060, 4050), Net.Income..Rs..1 = c(1250,
4560.72, 4762.5, -150, 2645, 2852.5)), .Names = c("ID", "Area..acres.",
"Cotton", "Pigeon.pea", "Soyabean", "Sorghum", "Other", "Total.yield..Quintal.",
"Gross.Income..Rs.", "Total.Expenditure..Rs.", "Net.Income..Rs.",
"Area..acres..1", "Cotton.1", "Pigeon.pea.1", "Soyabean.1", "Sorghum.1",
"Other.1", "Total.yield..Quintal..1", "Gross.Income..Rs..1",
"Total.Expenditure..Rs..1", "Net.Income..Rs..1"), row.names = c(NA,
6L), class = "data.frame"), structure(list(ID = c(1, 2, 3, 4,
5, 6), Area..acres. = c(2, 1, 2, 6, 1, 1), Cotton = c(NaN, 6,
2, 1.75, NaN, NaN), Pigeon.pea = c(0.75, 2, NaN, 1, NaN, NaN),
Soyabean = c(4, NaN, 1.5, 2.75, 3, 3), Sorghum = c(1, NaN,
2, 1, NaN, NaN), Other = c(0.25, 0.1, NaN, NaN, NaN, NaN),
Total.yield..Quintal. = c(12, 8.1, 11, 18.5, 3, 3), Gross.Income..Rs. = c(6525,
3000, 5575, 4666.67, 3240, 3800), Total.Expenditure..Rs. = c(3785,
2290, 2270, 2450, 1950, 2900), Net.Income..Rs. = c(2740,
710, 3305, 2216.67, 1290, 900), Area..acres..1 = c(3, 2,
2, 5, 4, 5), Cotton.1 = c(2, 2.5, 2, NaN, 0.83, 3), Pigeon.pea.1 = c(0.75,
0.25, 1, NaN, 1, 0.62), Soyabean.1 = c("", "", "", "", "",
""), Sorghum.1 = c(4, 0.1, 4, 1.5, 2, 3), Other.1 = c(NaN,
0.1, NaN, NaN, NaN, NaN), Total.yield..Quintal..1 = c(9.5,
5.7, 7, 3, 7.5, 17.5), Gross.Income..Rs..1 = c(4433.33, 3500,
4275, 1275, 2900, 5980), Total.Expenditure..Rs..1 = c(3823.33,
3270, 2660, 2075, 2262.5, 3560), Net.Income..Rs..1 = c(610,
230, 1615, 800, 637.5, 2420)), .Names = c("ID", "Area..acres.",
"Cotton", "Pigeon.pea", "Soyabean", "Sorghum", "Other", "Total.yield..Quintal.",
"Gross.Income..Rs.", "Total.Expenditure..Rs.", "Net.Income..Rs.",
"Area..acres..1", "Cotton.1", "Pigeon.pea.1", "Soyabean.1", "Sorghum.1",
"Other.1", "Total.yield..Quintal..1", "Gross.Income..Rs..1",
"Total.Expenditure..Rs..1", "Net.Income..Rs..1"), row.names = c(NA,
6L), class = "data.frame"), structure(list(ID = c(1, 2, 3, 4,
5, 6), Area..acres. = c(2, 1.5, 2, 3, 2, 3), Cotton = c(NaN,
2, NaN, 2.66, NaN, NaN), Pigeon.pea = c(NaN, 0.33, 0.4, 0.53,
0.5, NaN), Soyabean = c(3.5, NaN, 2, NaN, 3, 2.66), Sorghum = c(NaN,
NaN, NaN, 0.25, 2, NaN), Other = c(NaN, NaN, NaN, 0.19, NaN,
NaN), Total.yield..Quintal. = c(NaN, 3.5, 4.8, 10.02, 11, 8),
Gross.Income..Rs. = c(4200, 4646.66, 5389, 6507.33, 6600,
3200), Total.Expenditure..Rs. = c("1670", "2060", "2385",
"2528.33", "3006.5", "1426.66"), Net.Income..Rs. = c(2530,
2586.66, 3004, 3979, 3592.5, 1773.34), Area..acres..1 = c(2,
1.5, 2, 7, 1.5, 7.5), Cotton.1 = c(2, 3.33, 3, 2.16, 2, 0.93
), Pigeon.pea.1 = c(1, 0.33, 0.4, 0.33, 0.26, 0.2), Soyabean.1 = c(NaN,
NaN, NaN, NaN, NaN, NaN), Sorghum.1 = c(NaN, NaN, NaN, 5,
NaN, 6), Other.1 = c(NaN, NaN, NaN, NaN, NaN, NaN), Total.yield..Quintal..1 = c(6,
5.5, 6.8, 20, 3.4, 14.5), Gross.Income..Rs..1 = c(5930, 7500,
6920, 5100, 4666, 2822.66), Total.Expenditure..Rs..1 = c(3225,
3400, 3610, 3654.28, 5600, 1754.66), Net.Income..Rs..1 = c(2705,
4100, 3310, 1445.72, -934, 1068)), .Names = c("ID", "Area..acres.",
"Cotton", "Pigeon.pea", "Soyabean", "Sorghum", "Other", "Total.yield..Quintal.",
"Gross.Income..Rs.", "Total.Expenditure..Rs.", "Net.Income..Rs.",
"Area..acres..1", "Cotton.1", "Pigeon.pea.1", "Soyabean.1", "Sorghum.1",
"Other.1", "Total.yield..Quintal..1", "Gross.Income..Rs..1",
"Total.Expenditure..Rs..1", "Net.Income..Rs..1"), row.names = c(NA,
6L), class = "data.frame"), structure(list(ID = c(1, 2, 3, 4,
5, 6), Area..acres. = c(2, 2, 2, 3, 2, 1), Cotton = c(NaN, 3,
2, NaN, NaN, 4), Pigeon.pea = c(NaN, NaN, 0.5, 0.5, 1.5, 1),
Soyabean = c(3.66, NaN, NaN, 3, 1, NaN), Sorghum = c(2, NaN,
NaN, 1, 2.5, NaN), Other = c(22, NaN, NaN, 0.17, NaN, NaN
), Total.yield..Quintal. = c(12.3, 6, 5, 14.3, 10, 5), Gross.Income..Rs. = c(6030,
6420, 5562, 8183.33, 7780, 11800), Total.Expenditure..Rs. = c(3192,
4080, 3350, 20530, 3240, 5130), Net.Income..Rs. = c(2838,
2340, 2212, 5653.33, 4540, 6670), Area..acres..1 = c(2, 1,
2, 8, 1, 5), Cotton.1 = c(2, 4, 2.5, 3, 5.8, 5), Pigeon.pea.1 = c(3,
NaN, 0.5, 0.7, NaN, 0.25), Soyabean.1 = c(1, NaN, NaN, NaN,
NaN, NaN), Sorghum.1 = c(NaN, NaN, NaN, 3.7, NaN, 2), Other.1 = c(NaN,
NaN, NaN, NaN, NaN, NaN), Total.yield..Quintal..1 = c(0,
4, 6, 28, 5.8, 23), Gross.Income..Rs..1 = c(8675, 9760, 6677.5,
7417.7, 13050, 10860), Total.Expenditure..Rs..1 = c(7700,
6750, 5425, 4112.5, 6300, 4870), Net.Income..Rs..1 = c(975,
3010, 1252.5, 3305.7, 4750, 5990)), .Names = c("ID", "Area..acres.",
"Cotton", "Pigeon.pea", "Soyabean", "Sorghum", "Other", "Total.yield..Quintal.",
"Gross.Income..Rs.", "Total.Expenditure..Rs.", "Net.Income..Rs.",
"Area..acres..1", "Cotton.1", "Pigeon.pea.1", "Soyabean.1", "Sorghum.1",
"Other.1", "Total.yield..Quintal..1", "Gross.Income..Rs..1",
"Total.Expenditure..Rs..1", "Net.Income..Rs..1"), row.names = c(NA,
6L), class = "data.frame"))