2014-04-15 57 views
0

我是使用for循环的新手。我创建了一个棒球队的名单。在这些团队变量中有6个包含来自baseball-reference.com的数据的列表。我需要从第6列表中的第9列和第10列访问数据(这些只是我在完整代码中拥有的30个团队中的2个)。我的示例代码如下:在列表上运行For循环

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 

这是第一个两个列表中的我的变量结构:

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循环?

回答

1

或者,你可以不循环使用所有,但尝试lapply来代替:

b <- lapply(a, function(x) x[[6]][, c(9,10)]) 

这也是通常的名字,而不是数字引用列一个好主意。特别是如果您无法控制原始数据。然后,如果他们添加另一列,数字可能会改变,但名称可能不会。所以试试这个:

b <- lapply(a, function(x) x[["team_schedule"]][, c("R", "RA")]) 
+0

这很有道理。谢谢! –

0

我认为b应该是一个列表?在这种情况下,申报b <- list()循环之前的某个地方,并使用

for(i in 1:length(mylist)) { 
    b[[i]] <- a[[i]][[6]][, c(9, 10)] 
} 

注双括号。

+0

这使它完美的工作。谢谢您的帮助! –