如何从命名列表中提取特定元素?

时间:2015-06-09 15:02:23

标签: r

Gold15 <- read.csv("Gold15.csv")  
colnames(Gold15) <- c("Date", "Time", "Open", "High", "Low", "Close", "Volume")  
HammerFormed <- NULL  
PrecedingHammer <- NULL  
PostHammer <- NULL  
Profit <- NULL  

for (i in 1:nrow(Gold15)){  
  FULLBODY <- Gold15[i,"High"] - Gold15[i,"Low"]  
  LOWERSHADOW <- Gold15[i, "Open"] - Gold15[i, "Low"]  
  UPPERSHADOW <- Gold15[i, "High"] - Gold15[i, "Close"]  
  CANDLEBODY <- Gold15[i, "Close"] - Gold15[i, "Open"]  


  if (FULLBODY >= 3*CANDLEBODY && UPPERSHADOW <= 0.05*FULLBODY && LOWERSHADOW >= 0.6*FULLBODY){  
HammerFormed <<- c(HammerFormed, Gold15[i,c("Date","Time", "Open","High","Low","Close","Volume"]))  
PrecedingHammer <<- c(PrecedingHammer, Gold15[i-4,c("Date","Time", "Open","High","Low","Close","Volume")])  
PostHammer <<- c(PostHammer, Gold15[i+4, c("Date","Time", "Open","High","Low","Close","Volume")])  
Profit <<- c(Profit, (PostHammer$Close - HammerFormed$Close))
  }

}

所以在这段代码中,PrecedingHammer&amp; PostHammer被命名为列表。但是,当我尝试使用

访问这两个元素时
PostHammer$Close[2]

PostHammer[[6]][2]

控制台返回NA。 这是PostHammer的str()用于分析:str(PostHammer) List of 462 $ Date : Factor w/ 142 levels "2014.10.13","2014.10.14",..: 5 $ Time : Factor w/ 96 levels "00:00","00:15",..: 57 $ Open : num 1239 $ High : num 1239 $ Low : num 1239 $ Close : num 1239 $ Volume: int 4254

有人可以告诉我如何访问此列表中的元素吗?

编辑:我在这里上传了文件Gold15.csv:Gold15.csv

github link here

1 个答案:

答案 0 :(得分:0)

原始代码导致对象具有您显然没有意图的维度,并且2个感兴趣的对象作为非常长的列表出现(并且没有以可能有用的方式嵌套)。

例如,对于每个迭代都有一个单独的单值列表元素,而不是像对象的元素在这些对象中具有多个值ClosedClose,{{ 1}},Close.2

Close.3

我所做的高层次是:

  1. 使用数据框而非列表,这些更灵活,允许我轻松创建具有您期望的尺寸的对象 1B。将对象初始化为更合适的维度和类
  2. 创建一个索引,指示哪些数据行符合您的标准,以避免可能留下间隙或以其他方式中断的循环
  3. 将它分成更小的步骤以确保我们知道发生了什么并消除了错误
  4. 看到滞后/超前逻辑必然导致所有> Gold15 <- read.csv("Gold15.csv") > colnames(Gold15) <- c("Date", "Time", "Open", "High", "Low", "Close", "Volume") > HammerFormed <- data.frame(matrix(nrow= nrow(Gold15), ncol = 7)) > PrecedingHammer <- data.frame(matrix(nrow= 1, ncol = 7)) > PostHammer <- data.frame(matrix(nrow= 1, ncol = 7)) > Profit <- data.frame(matrix(nrow= nrow(Gold15), ncol = 1)) > > for (i in 1:nrow(Gold15)){ + FULLBODY[i] <- Gold15[i,"High"] - Gold15[i,"Low"] + LOWERSHADOW[i] <- Gold15[i, "Open"] - Gold15[i, "Low"] + UPPERSHADOW[i] <- Gold15[i, "High"] - Gold15[i, "Close"] + CANDLEBODY[i] <- Gold15[i, "Close"] - Gold15[i, "Open"] + } > > condition <- data.frame(matrix(nrow = length(FULLBODY), data = 0)) > for(i in 1: length(FULLBODY)){ + if(FULLBODY[i] >= 3*CANDLEBODY[i] && UPPERSHADOW[i] <= + 0.05*FULLBODY[i] && LOWERSHADOW[i] >= 0.6*FULLBODY[i]) + { + condition[i,1] <- 1 + } + } > table(condition) condition 0 1 10714 66 > > HammerFormed <- Gold15[condition[,1],] > > for(i in 1:nrow(condition)){ + if(condition[i,1] == 1){ + PrecedingHammer[i,] <- Gold15[i-4, ] + } + } > > # Remove NA's > PrecedingHammer <- PrecedingHammer[!(is.na(PrecedingHammer[,1])),] > > for(i in 1:nrow(condition)){ + if(condition[i,1] == 1){ + PostHammer[i,] <- Gold15[i-4, ] + } + } > > # Remove NA's > PostHammer <- PostHammer[!(is.na(PostHammer[,1])),] > > names(PostHammer) <- colnames(Gold15) > PostHammer$Close[2] [1] 1242.96 > 值的某些行随后消除了那些行