我无法将列表中 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)
b <- lapply(a, function(x) x[["team_schedule"]][, c("R", "RA")])
c <- as.numeric(as.character(b))
当我运行此代码时,我得到:
> c
[1] NA NA
> str(c)
num [1:2] NA NA
b的结构如下:
> str(b)
List of 2
$ :'data.frame': 165 obs. of 2 variables:
..$ R : Factor w/ 13 levels "","0","10","11",..: 6 6 7 8 10 7 6 5 9 2 ...
..$ RA: Factor w/ 13 levels "","0","1","10",..: 3 9 7 4 10 3 7 8 7 6 ...
$ :'data.frame': 166 obs. of 2 variables:
..$ R : Factor w/ 10 levels "","0","1","2",..: 3 8 6 4 8 2 7 9 6 3 ...
..$ RA: Factor w/ 13 levels "","1","10","14",..: 5 5 6 9 10 7 2 3 5 7 ...
我应该怎么做才能将因子转换为数值?