标签:var 字符串操作 保存到文件 utf-8 特定 false 字符串截取 read header
安装所需的包:
install.packages("xxxx")
载入包:
library("xxxx")
显示/改变工作文件夹
getwd()/setwd("d:/temp")
文件操作:
1.读取csv文件
var=read.csv("demo.csv",header=FALSE)——header表示是否有表头。默觉得TRUE
2.读取数据文件
data=read.table("demo.data")
3.读取文本文件
var=readLines("demo.txt",encoding="UTF-8")
4.输出csv文件
write.csv(var,"var.csv")
5.输出普通文本文件
write(var,"var.txt")
6.把整个执行空间保存到文件
save.image("demo.RData")
7.读取文件到执行空间
load("demo.RData")
清除执行空间所有变量:
rm(list=ls())
字符串操作:
1.获取字符串长度
nchar("asdf")——与length是不同的
2.连接字符串
paste("abc","def")——默认用空格间隔。若取消间隔。可用參数sep=""
3.切割字符串
str1=strsplit("abc def"," ")——切割结果是list类型,如需转成向量使用str1=unlist(strsplit("abc def"," "))
4.字符串截取
substr("hello",2,3)——參数表示起止位置
5.字符串替代
gsub("a","c","asdfasdf")——把asdfasdf替换成csdfcsdf
6.字符串匹配
grep("oo",c("hello","world","good"))——搜索给定字符串中的特定表达式 。返回其位置索引
集合(向量)操作:
1.求交集
intersect(c("a","b","c"),c("a","c","d"))
2.求并集
union(c("a","b","c"),c("a","c","d"))
3.求差集
setdiff(c("a","b","c"),c("a","c","d"))
4.向量去重
unique(c("a","b","a","c"))
5.向量排序
sort(c("a","b","a","c"))/rev(sort(c("a","b","a","c")))
标签:var 字符串操作 保存到文件 utf-8 特定 false 字符串截取 read header
原文地址:http://www.cnblogs.com/clnchanpin/p/6815679.html