从CSV文件中读取数据帧中的级别 - R [英] Removing levels from data frame read from CSV file - R
问题描述
我尝试从此链接加载棒球统计资料。当我从文件中使用
I tried loading the baseball statistics from this link. When I read it from the file using
data <- read.csv("MLB2011.csv")
似乎正在读取所有字段作为因子值。我尝试删除这些因子值:
it seems to be reading all fields as factor values. I tried dropping those factor values by doing:
read.csv("MLB2011.xls", as.is= FALSE)
..但是看起来这些值仍然被读取为因素。我可以做些什么来加载它们作为简单的字符值而不是因素?
.. but it looks like the values are still being read as factors. What can I do to have them loaded as simple character values and not factors?
推荐答案
你不是在读一个 csv
文件,它是一个excel电子表格(.xls格式)。它包含两个工作表 bat2011
和 pitch2011
You aren't reading a csv
file, it is an excel spreadsheet (.xls format). It contains two worksheets bat2011
and pitch2011
你可以使用 XLConnect
库来阅读此
library(XLConnect)
# load the work book (connect to the file)
wb <- loadWorkbook("MLB2011.xls")
# read in the data from the bat2011 sheet
bat2011 <- readWorksheet(wb, sheet = 'bat2011')
readWorksheet
有一个参数 colType
可用于指定列类型。
readWorksheet
has an argument colType
which you could use to specify the column types.
编辑
如果您已将表格保存为csv文件,则
If you have already saved the sheets as csv files then
as.is = TRUE
或 stringsAsFactors = FALSE
将是正确的参数值
这篇关于从CSV文件中读取数据帧中的级别 - R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!