设置文件目录
读取数据csv,“,”csv的分隔符,注意表格中不能有逗号
一般仅要设施以下参数
result <- read.table(file, header = TRUE,sep = “,”,stringsAsFactors = FALSE)
setwd("C:/Users/xikun/Desktop/10.26all in")
result <- read.table('LC1917_20201224_1.csv', header = TRUE,sep = ",",stringsAsFactors = FALSE)
报错
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
line N did not have N elements
一般是由于你的csv某个单元格中存在逗号,而csv以逗号分隔,多分了好多行
read的其他参数
read.table(file, header = FALSE, sep = “”, quote = “”’", dec = “.”, numerals = c(“allow.loss”, “warn.loss”, “no.loss”), row.names, col.names, as.is = !stringsAsFactors, na.strings = “NA”, colClasses = NA, nrows = -1, skip = 0, check.names = TRUE, fill = !blank.lines.skip, strip.white = FALSE, blank.lines.skip = TRUE, comment.char = “#”, allowEscapes = FALSE, flush = FALSE, stringsAsFactors = default.stringsAsFactors(), fileEncoding = “”, encoding = “unknown”, text, skipNul = FALSE) read.csv(file, header = TRUE, sep = “,”, quote = “”", dec = “.”, fill = TRUE, comment.char = “”, …) read.csv2(file, header = TRUE, sep = “;”, quote = “”", dec = “,”, fill = TRUE, comment.char = “”, …) read.delim(file, header = TRUE, sep = “\t”, quote = “”", dec = “.”, fill = TRUE, comment.char = “”, …) read.delim2(file, header = TRUE, sep = “\t”, quote = “”", dec = “,”, fill = TRUE, comment.char = “”, …)
Arguments
file
要打开的文件的名称,或是包含数据的连接,甚至可以用URL
header
逻辑值,设置文件的第一行是否是变量名.
sep
用于分隔字段的一个或若干个字符。如果设置的是“”,则所有空白都会被视为分隔符.
quote
如果字符值是在引号中的,这个参数就可以设置引号的类型.
dec
小数点的符号.
numerals
string indicating how to convert numbers whose conversion to double precision would lose accuracy, see type.convert. Can be abbreviated.
row.names
字符向量,代表返回的数据框的行名称
col.names
字符向量,代表返回的数据框的列名称.
as.is
逻辑向量(长度和列数一样),设置是否将字符转换为因子.
na.strings
字符向量,设置被视为NA 的值.
colClasses
字符向量,每列的类名称
nrows
整数值,要读取的行数.
skip
整数值,设置在开始读取数据前要跳过的文件中的多少行内容.
check.names
逻辑值,设置read.table是否要检查列名称是否是R种有效的符号名称.
fill
逻辑值,设置是否应该在某些值缺失的情况下,自动地添加空白字段.
strip.white
逻辑值,是否将字符字段前后的空白删除.
blank.lines.skip
逻辑值,是否忽略空行.
comment.char
设置注释符号,以便输入文件时自动跳过以某个特定符号开始的注释行
allowEscapes
逻辑值,设置是否使用转义符,还是直接读入.
flush
逻辑值,设置所有字段都已读入的情况下,是否应该跳到下一行.
stringsAsFactors
逻辑值,设置文本字段是否应该被转换成因子.
fileEncoding
character string: if non-empty declares the encoding used on a file (not a connection) so the character data can be re-encoded. See the ‘Encoding’ section of the help for file, the ‘R Data Import/Export Manual’ and ‘Note’.
encoding
源文件的编码方式.
text
character string: if file is not supplied and this is, then data are read from the value of text via a text connection. Notice that a literal string can be used to include (small) data sets within R code.
skipNul
logical: should nuls be skipped?
…
Further arguments to be passed to read.table.
See Also
The ‘R Data Import/Export’ manual.
scan, type.convert, read.fwf for reading fixed width formatted input; write.table;data.frame.
count.fields can be useful to determine problems with reading files which result in reports of incorrect record lengths (see the ‘Examples’ below).
http://tools.ietf.org/html/rfc4180 for the IANA definition of CSV files (which requires comma as separator and CRLF line endings).
Examples
## using count.fields to handle unknown maximum number of fields ## when fill = TRUE test1 <- c(1:5, “6,7”, “8,9,10”) tf <- tempfile() writeLines(test1, tf) read.csv(tf, fill = TRUE) # 1 column ncol <- max(count.fields(tf, sep = “,”)) read.csv(tf, fill = TRUE, header = FALSE, col.names = paste0(“V”, seq_len(ncol))) unlink(tf) ## “Inline” data set, using text= ## Notice that leading and trailing empty lines are auto-trimmed read.table(header = TRUE, text = " a b 1 2 3 4 ")