项目场景:
使用R读取csv时发现第一列被当成了数据,使用的read.csv函数,忘记了之前是怎么使用的。因此自己记录一下。
原因分析
read.csv(file, header = TRUE, sep = “,”, quote = “”",
dec = “.”, fill = TRUE, comment.char = “”, …)
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 = FALSE,
fileEncoding = “”, encoding = “unknown”, text, skipNul = FALSE)
row.names 这个参数是设置行名,设置成数据的第一列
a vector of row names. This can be a vector giving the actual row names, or a single number giving the column of the table which contains the row names, or character string giving the name of the table column containing the row names.
If there is a header and the first row contains one fewer field than the number of columns, the first column in the input is used for the row names. Otherwise if row.names is missing, the rows are numbered.
Using row.names = NULL forces row numbering. Missing or NULL row.names generate row names that are considered to be ‘automatic’ (and not preserved by as.matrix).
解决方案:
marketing<-read.csv(file="D:\\jupyter\\华数杯\\df_data.csv",row.names=1)