1、数据的导入
导入文本文件
使用read.table函数导入普通文本文件
read.table(file,header=FALSE,sep="",...)
#导入csv文件
data1 <- read.table("1.csv", header=TRUE, sep=",", fileEncoding="UTF-8", stringsAsFactors=FALSE);
data2 <- read.csv("2.txt", header=TRUE, sep=",");
#不带表头
data2 <- read.csv("2.txt", header=FALSE, sep=",", col.names=c("age", "name"));
data3 <- read.csv("3.xxx", header=FALSE, sep=",");
#指定分隔符
data3 <- read.csv("3.xxx", header=FALSE, sep="\t")
2、数据的导出
导出文本文件
write.table(x,file="",sep="",row.names=TRUE,col.names=TRUE,quote=TRUE)
age <- c(22, 23);
name <- c('KEN', 'John');
cname <- c("小明", "小刚");
f <- data.frame(age, name, cname, stringsAsFactors = FALSE);
write.table(f, file='f.csv', sep=',', fileEncoding = "UTF-8")
#去除行号
write.table(f, file='f.csv', sep=',', fileEncoding = "UTF-8", row.names=FALSE)
#去除列名
write.table(f, file='f.csv', sep=',', fileEncoding = "UTF-8", row.names=FALSE, col.names=FALSE)
#去除引号
write.table(f, file='f.csv', sep=',', fileEncoding = "UTF-8",
quote = FALSE, row.names=FALSE, col.names=FALSE)