我是使用for循环的新手。我创建了一个棒球队名单。在这些团队变量中有6个列表,其中包含来自baseball-reference.com的数据。我需要访问第6个列表中第9和第10列的数据(这些只是我将在完整代码中拥有的30个团队中的2个)。我的示例代码如下:
library(XML)
bal <- "http://www.baseball-reference.com/teams/BAL/2014-schedule-scores.shtml"
bos <- "http://www.baseball-reference.com/teams/BOS/2014-schedule-scores.shtml"
mylist <- list(bal, bos)
a <- lapply(mylist, readHTMLTable)
for(i in 1:length(mylist)) {
b[i] <- a[[i]][[6]][, c(9, 10)]
}
当我运行此代码时出现错误:
Warning messages:
1: In b[i] <- a[[i]][[6]][, c(9, 10)] :
number of items to replace is not a multiple of replacement length
2: In b[i] <- a[[i]][[6]][, c(9, 10)] :
number of items to replace is not a multiple of replacement length
以下是变量a中两个列表中第一个的结构:
List of 2
$ :List of 6
..$ NULL :'data.frame': 13 obs. of 3 variables:
.. ..$ V1: Factor w/ 13 levels "Â","Biggest Lead",..: 3 1 2 4 8 9 7 6 10 11 ...
.. ..$ V2: Factor w/ 10 levels "0","1","11","12",..: 2 3 10 7 2 8 6 9 3 5 ...
.. ..$ V3: Factor w/ 12 levels "","Â","last on Apr 1, counts all days of the season including off days",..: 6 3 1 7 5 4 11 12 9 10 ...
..$ NULL :'data.frame': 16 obs. of 6 variables:
.. ..$ V1: Factor w/ 9 levels "Blowouts (5+ Runs)",..: 8 9 4 9 5 9 2 9 3 9 ...
.. ..$ V2: Factor w/ 1 level "W": NA 1 NA 1 NA 1 NA 1 NA 1 ...
.. ..$ V3: Factor w/ 1 level "L": NA 1 NA 1 NA 1 NA 1 NA 1 ...
.. ..$ V4: Factor w/ 1 level "RS": NA 1 NA 1 NA 1 NA 1 NA 1 ...
.. ..$ V5: Factor w/ 1 level "RA": NA 1 NA 1 NA 1 NA 1 NA 1 ...
.. ..$ V6: Factor w/ 1 level "WP": NA 1 NA 1 NA 1 NA 1 NA 1 ...
..$ NULL :'data.frame': 5 obs. of 6 variables:
.. ..$ V1: Factor w/ 3 levels "Home/Road","Month by Month",..: 3 1 3 2 3
.. ..$ V2: Factor w/ 1 level "W": 1 NA 1 NA 1
.. ..$ V3: Factor w/ 1 level "L": 1 NA 1 NA 1
.. ..$ V4: Factor w/ 1 level "RS": 1 NA 1 NA 1
.. ..$ V5: Factor w/ 1 level "RA": 1 NA 1 NA 1
.. ..$ V6: Factor w/ 1 level "WP": 1 NA 1 NA 1
..$ NULL :'data.frame': 7 obs. of 6 variables:
.. ..$ V1: Factor w/ 4 levels "Blowouts (5+ Runs)",..: 4 2 4 3 4 1 4
.. ..$ V2: Factor w/ 1 level "W": 1 NA 1 NA 1 NA 1
.. ..$ V3: Factor w/ 1 level "L": 1 NA 1 NA 1 NA 1
.. ..$ V4: Factor w/ 1 level "RS": 1 NA 1 NA 1 NA 1
.. ..$ V5: Factor w/ 1 level "RA": 1 NA 1 NA 1 NA 1
.. ..$ V6: Factor w/ 1 level "WP": 1 NA 1 NA 1 NA 1
..$ NULL :'data.frame': 1 obs. of 6 variables:
.. ..$ V1: Factor w/ 1 level "Split": 1
.. ..$ V2: Factor w/ 1 level "W": 1
.. ..$ V3: Factor w/ 1 level "L": 1
.. ..$ V4: Factor w/ 1 level "RS": 1
.. ..$ V5: Factor w/ 1 level "RA": 1
.. ..$ V6: Factor w/ 1 level "WP": 1
..$ team_schedule:'data.frame': 166 obs. of 21 variables:
.. ..$ Rk : Factor w/ 163 levels "1","10","100",..: 1 75 86 97 108 119 130 141 152 2 ...
.. ..$ Gm# : Factor w/ 163 levels "1","10","100",..: 1 75 86 97 108 119 130 141 152 2 ...
.. ..$ Date : Factor w/ 163 levels "Date","Friday, Apr 11",..: 40 140 100 5 50 76 30 118 143 2 ...
.. ..$ : Factor w/ 2 levels "","boxscore": 2 2 2 2 2 2 2 2 2 2 ...
.. ..$ Tm : Factor w/ 2 levels "BAL","Tm": 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ Â : Factor w/ 3 levels "","@","Â": 1 1 1 2 2 2 2 2 2 1 ...
.. ..$ Opp : Factor w/ 21 levels "BOS","CHC","CHW",..: 1 1 1 6 6 6 12 12 12 20 ...
.. ..$ : Factor w/ 24 levels "","1:05 pm","1:07 pm",..: 24 23 23 23 23 24 23 24 24 23 ...
.. ..$ R : Factor w/ 10 levels "","0","14","2",..: 4 4 5 6 8 5 4 3 7 2 ...
.. ..$ RA : Factor w/ 10 levels "","1","10","11",..: 2 8 6 3 9 2 6 7 6 5 ...
.. ..$ Inn : Factor w/ 2 levels "","12": 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ W-L : Factor w/ 13 levels "1-0","1-1","1-2",..: 1 2 3 4 5 6 7 8 9 10 ...
.. ..$ Rank : Factor w/ 5 levels "1","2","3","4",..: 1 2 4 5 5 4 5 4 3 5 ...
.. ..$ GB : Factor w/ 6 levels "0.5","1.0","1.5",..: 6 1 2 3 5 3 4 3 2 3 ...
.. ..$ Win : Factor w/ 11 levels "Britton","Buehrle",..: 1 6 4 10 9 11 5 3 7 8 ...
.. ..$ Loss : Factor w/ 11 levels "Archer","Chen",..: 6 4 2 3 7 11 4 8 5 10 ...
.. ..$ Save : Factor w/ 6 levels "","Hunter","Kelley",..: 2 1 6 1 4 2 3 1 2 5 ...
.. ..$ Time : Factor w/ 13 levels "2:20","2:34",..: 6 3 7 10 11 2 8 12 9 1 ...
.. ..$ D/N : Factor w/ 2 levels "D","N": 1 2 2 1 1 1 1 1 2 2 ...
.. ..$ Attendance: Factor w/ 13 levels "15,799","20,880",..: 12 5 2 4 7 8 13 9 11 3 ...
.. ..$ Streak : Factor w/ 6 levels "-","--","---",..: 5 1 2 3 4 5 1 5 6 1 ...
如何解决此问题?我是否使用for循环使用我的方法在正确的轨道上?
答案 0 :(得分:1)
或者,您根本不能使用for循环,而是尝试使用lapply
:
b <- lapply(a, function(x) x[[6]][, c(9,10)])
通过名称而不是数字来引用列通常也是一个好主意。特别是如果您无法控制原始数据。然后,如果他们添加另一列,数字可能会改变,但名称可能会赢得。所以试试这个:
b <- lapply(a, function(x) x[["team_schedule"]][, c("R", "RA")])
答案 1 :(得分:0)
我认为b
也应该是一个列表?在这种情况下,在循环之前的某处声明b <- list()
,然后使用
for(i in 1:length(mylist)) {
b[[i]] <- a[[i]][[6]][, c(9, 10)]
}
注意双括号。