首页 > 解决方案 > 使用 R 循环多个变量

问题描述

我从 CSV 文件导入数据,并希望在 2018 年公司股票的两个价格之间创建一个“比较表”:

Table2018<-data.frame("Comparison"= c("Opening Price bigger than Adjusted Closing Price",
"Opening Pricee smaller than Adjusted Closing Price","Total trading days"),
"January18","February18",
"March18","April18","May18","June18","July18"
,"August18","September18","October18",
"November18","December18",stringsAsFactors = FALSE)

我有这组代码(所有比较):

Table2018[1,2]<-sum(January18$Opening.Price > January18$Adjusted.Closing.Price)
Table2018[1,3]<-sum(February18$Opening.Price > February18$Adjusted.Closing.Price)
Table2018[1,4]<-sum(March18$Opening.Price > March18$Adjusted.Closing.Price)
Table2018[1,5]<-sum(April18$Opening.Price > April18$Adjusted.Closing.Price)
Table2018[1,6]<-sum(May18$Opening.Price > May18$Adjusted.Closing.Price)
Table2018[1,7]<-sum(June18$Opening.Price > June18$Adjusted.Closing.Price)
Table2018[1,8]<-sum(July18$Opening.Price > July18$Adjusted.Closing.Price)
Table2018[1,9]<-sum(August18$Opening.Price > August18$Adjusted.Closing.Price)
Table2018[1,10]<-sum(September18$Opening.Price > September18$Adjusted.Closing.Price)
Table2018[1,11]<-sum(October18$Opening.Price > October18$Adjusted.Closing.Price)
Table2018[1,12]<-sum(November18$Opening.Price > November18$Adjusted.Closing.Price)
Table2018[1,13]<-sum(December18$Opening.Price > December18$Adjusted.Closing.Price)

对于那些问的人,这是最后的代码部分和我看起来很糟糕的表格:

Total.trading.days <- c(length(January18$ן..Date),length(February18$ן..Date),length(March18$ן..Date),length(April18$ן..Date),length(May18$ן..Date),length(June18$ן..Date),length(July18$ן..Date),length(August18$ן..Date),length(September18$ן..Date),length(October18$ן..Date),length(November18$ן..Date),length(December18$ן..Date))

#Displaying finished table
for (i in 1:12) {
  Table2018[3,i+1]<-Total.trading.days[i]
  Table2018[2,i+1]<-Total.trading.days[i]-as.numeric(Table2018[1,i+1])
}
Table2018

                                          Comparison
1   Opening Price bigger than Adjusted Closing Price
2 Opening Pricee smaller than Adjusted Closing Price
3                                 Total trading days
  X.January18. X.February18. X.March18. X.April18.
1           20            17         17         13
2            1             1          1          4
3           21            18         18         17
  X.May18. X.June18. X.July18. X.August18.
1       19        18        14          18
2        1         0         2           2
3       20        18        16          20
  X.September18. X.October18. X.November18.
1              8           17            19
2              3            2             0
3             11           19            19
  X.December18.
1            16
2             4
3            20

dput(head(Table2018))

structure(list(Comparison = c("Opening Price bigger than Adjusted Closing Price", 
"Opening Pricee smaller than Adjusted Closing Price", "Total trading days"
), X.January18. = c("20", "1", "21"), X.February18. = c("17", 
"1", "18"), X.March18. = c("17", "1", "18"), X.April18. = c("13", 
"4", "17"), X.May18. = c("19", "1", "20"), X.June18. = c("18", 
"0", "18"), X.July18. = c("14", "2", "16"), X.August18. = c("18", 
"2", "20"), X.September18. = c("8", "3", "11"), X.October18. = c("17", 
"2", "19"), X.November18. = c("19", "0", "19"), X.December18. = c("16", 
"4", "20")), row.names = c(NA, 3L), class = "data.frame")
  1. 问题主要是代码太多。在代码的第二部分,我怎样才能制作一个漂亮的循环?我需要一个吗?
  2. 为什么我在表格的标题中出现这种格式:X.month.
  3. 我也很想知道如何更漂亮地展示我的桌子

标签: rloops

解决方案


推荐阅读