R可以读取本地的文档,还有数据库的文档等
> #控制台的输入
> x <- readline()
user
> x
[1] "user"
> #user 到x里面去了
> x <- scan()
1: 3.1
2: 2.3
3: 4.5
4:
Read 3 items
> #本地文档的输出
> #创立连接
> output <- file('output.txt')
> #查看属性
> class(output)
[1] "file" "connection"
> #cat函数本来是用来输出到控制台的
> cat(1:100)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
> 喜爱你在连接到文本去
Error: object '喜爱你在连接到文本去' not found
> #现在连接到文本去
> cat(1:100,seq = '\t',file = output)
> #查看目录在哪里
> getwd()
[1] "D:/Documents"
> 关闭连接
Error: object '关闭连接' not found
> #关闭连接
> close(output)
> #创立连接
> output <- file('output.txt')
> input <- scan(file = output)
Read 100 items
> #关闭连接
> close(output)
> head(input)
[1] 1 2 3 4 5 6
scan的默认输入是数值
处理字符串的输入输出问题
> output <- file('output.txt')
> writeLines(as.character(1:12),con = output)
> #用writeline函数来创立连接
> input <- readLines(output)
> input
[1] "1" "2" "3" "4" "5" "6" "7" "8"
[9] "9" "10" "11" "12"
> #内置的iris数据
> head(iris)
Sepal.Length Sepal.Width Petal.Length
1 5.1 3.5 1.4
2 4.9 3.0 1.4
3 4.7 3.2 1.3
4 4.6 3.1 1.5
5 5.0 3.6 1.4
6 5.4 3.9 1.7
Petal.Width Species
1 0.2 setosa
2 0.2 setosa
3 0.2 setosa
4 0.2 setosa
5 0.2 setosa
6 0.4 setosa
> write.table(iris,file = 'iris.csv',seq = ',')
Error in write.table(iris, file = "iris.csv", seq = ",") :
unused argument (seq = ",")
> write.table(iris,file = 'iris.csv',sep = ',')
> data <- read.table(file = 'iris.csv',sep = ',')
> head(data)
Sepal.Length Sepal.Width Petal.Length
1 5.1 3.5 1.4
2 4.9 3.0 1.4
3 4.7 3.2 1.3
4 4.6 3.1 1.5
5 5.0 3.6 1.4
6 5.4 3.9 1.7
Petal.Width Species
1 0.2 setosa
2 0.2 setosa
3 0.2 setosa
4 0.2 setosa
5 0.2 setosa
6 0.4 setosa
>
读取数据库,这段内容还不是非常的熟悉,以后再说
网页方面的读取