R provides several functions for accesssing the keyboard and monitor. Here, we’ll look at the scan(), readline(), print(), and cat() functions.
You can use scan() to read in a vector, whether numeric or character, from a file or the keyboard.
a<-scan("")
a
## numeric(0)
scan("C:/Users/pradeep/OneDrive/R Programming 2019-20/programs/def.txt")
## [1] 1 2 3 4 5 6 7 8 9
scan("C:/Users/pradeep/OneDrive/R Programming 2019-20/programs/abc.txt",what = "")
## [1] "AB" "C" "DEF" "G" "H"
By default the separator is space. But we can change it explicitly.
scan("C:/Users/pradeep/OneDrive/R Programming 2019-20/programs/abc.txt",what = "",sep = "\n")
## [1] "AB C" "DEF G H"
If you want to read in a single line from the keyboard, readline() is very handy.
w<-readline()
w
## [1] ""
w<-readline("Type your name:")
## Type your name:
w
## [1] ""
you can print the value of a variable or expression by simply typing the variable name or expression.
a<-5
a
## [1] 5
#(or)
print(a)
## [1] 5
It’s a little better to use cat() instead of print(), as the latter can print only one expression and its output is numbered, which may be a nuisance. Compare the results of the functions:
print("Hello")
## [1] "Hello"
cat("Hello\n")
## Hello
“” is generally using with cat(). Without it our next call would continue to write to the same line.
The arguments to cat() will be printed out with intervening spaces:
x=c(1,3)
cat(x,"abc","de\n")
## 1 3 abc de
If you don’t want the spaces, set sep to the empty string "", as follows:
cat(x,"abc","de\n",sep="")
## 13abcde
Any string can be used for sep. Here, we use the newline character
cat(x,"abc","de\n",sep="\n")
## 1
## 3
## abc
## de
You can even set sep to be a vector of strings, like this:
x <- c(5,12,13,8,88)
cat(x,sep=c(".",".",".","\n","\n"))
## 5.12.13.8
## 88
The first line contains an optional header, specifying column names. We could read the file this way:
read.table("C:/Users/pradeep/OneDrive/R PROGRAMMING/hi.txt")
## V1 V2
## 1 name age
## 2 John 25
## 3 Mary 28
## 4 jin 19
There is no different command for reading a matrix. We should read the matrix row by row.
matrix(scan("C:/Users/pradeep/OneDrive/R PROGRAMMING/matri.txt"),nrow=5,byrow=TRUE)
## [,1]
## [1,] 101
## [2,] 111
## [3,] 110
## [4,] 110
## [5,] 101
We opened the connection, assigned the result to a, and then read the file one line at a time, as specified by the argument n=1. When R encountered the end of file (EOF), it returned an empty result. We needed to set up a connection so that R could keep track of our position in the file as we read through it. We can detect EOF in our code.
a<-file("C:/Users/pradeep/OneDrive/R PROGRAMMING/hi.txt","r")
a
## A connection with
## description "C:/Users/pradeep/OneDrive/R PROGRAMMING/hi.txt"
## class "file"
## mode "r"
## text "text"
## opened "opened"
## can read "yes"
## can write "no"
readLines(a,n=1) # Reading one line at a time
## [1] "name age"
readLines(a,n=1) # Reading one line at a time
## [1] "John 25"
readLines(a,n=2) # reading 2 lines at a time by giving n=2( number of lines to read)
## [1] "Mary 28" "jin 19"
readLines(a,n=1)
## [1] ""
# Nothing is present to read
a<-file("C:/Users/pradeep/OneDrive/R PROGRAMMING/hi.txt","r")
while(TRUE){
r1<-readLines(a,n=1)
if(length(r1)==0)
{
print("reached the end of the file")
break
}else print(r1)
}
## [1] "name age"
## [1] "John 25"
## [1] "Mary 28"
## [1] "jin 19"
## [1] ""
## [1] ""
## [1] "reached the end of the file"
kids <- c("Jack","Jill")
ages <- c(12,10)
d <- data.frame(kids,ages,stringsAsFactors=FALSE)
d
## kids ages
## 1 Jack 12
## 2 Jill 10
write.table(d,"kds.txt")
In the case of writing a matrix to a file, just state that you do not want row or column names, as follows:
m<-matrix(1:9,ncol=3)
m
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
write.table(m,"C:/Users/pradeep/OneDrive/R PROGRAMMING/witematrix.txt",row.names = FALSE,col.names = FALSE)
cat("I K Pradeep\n",file="C:/Users/pradeep/OneDrive/R PROGRAMMING/mydetails.txt")
cat("Associate Professor\n",50000,file="C:/Users/pradeep/OneDrive/R PROGRAMMING/mydetails.txt",append = TRUE)
The first call to cat() creates the file , consisting of one line with contents “I K Pradeep”. The second call appends a second line “Associate Professor” and salary. NOTE: We have given both character and number at a time
x<-file("C:/Users/pradeep/OneDrive/R PROGRAMMING/wtlne.txt","w") # Here "w" specifies write
writeLines("Vishnu Institute of Technology::Bhimavaram",x)
close(x)
Now, lets close the connections
close(a)
uci <- read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data",header=FALSE)
Now. print the dataframe.
head(uci)
## V1 V2 V3 V4 V5 V6
## 1 39 State-gov 77516 Bachelors 13 Never-married
## 2 50 Self-emp-not-inc 83311 Bachelors 13 Married-civ-spouse
## 3 38 Private 215646 HS-grad 9 Divorced
## 4 53 Private 234721 11th 7 Married-civ-spouse
## 5 28 Private 338409 Bachelors 13 Married-civ-spouse
## 6 37 Private 284582 Masters 14 Married-civ-spouse
## V7 V8 V9 V10 V11 V12 V13
## 1 Adm-clerical Not-in-family White Male 2174 0 40
## 2 Exec-managerial Husband White Male 0 0 13
## 3 Handlers-cleaners Not-in-family White Male 0 0 40
## 4 Handlers-cleaners Husband Black Male 0 0 40
## 5 Prof-specialty Wife Black Female 0 0 40
## 6 Exec-managerial Wife White Female 0 0 40
## V14 V15
## 1 United-States <=50K
## 2 United-States <=50K
## 3 United-States <=50K
## 4 United-States <=50K
## 5 Cuba <=50K
## 6 United-States <=50K
Now, let’s give some column names
df<-c("age","workclass","fnlwgt","education","education-num","marital-status","occupation","relationship","race","sex","capital-gain","capital-loss","hours-per-week","native-country","income")
colnames(uci)<-df # Giving colomn names
Again print the dataframe.
head(uci)
## age workclass fnlwgt education education-num
## 1 39 State-gov 77516 Bachelors 13
## 2 50 Self-emp-not-inc 83311 Bachelors 13
## 3 38 Private 215646 HS-grad 9
## 4 53 Private 234721 11th 7
## 5 28 Private 338409 Bachelors 13
## 6 37 Private 284582 Masters 14
## marital-status occupation relationship race sex
## 1 Never-married Adm-clerical Not-in-family White Male
## 2 Married-civ-spouse Exec-managerial Husband White Male
## 3 Divorced Handlers-cleaners Not-in-family White Male
## 4 Married-civ-spouse Handlers-cleaners Husband Black Male
## 5 Married-civ-spouse Prof-specialty Wife Black Female
## 6 Married-civ-spouse Exec-managerial Wife White Female
## capital-gain capital-loss hours-per-week native-country income
## 1 2174 0 40 United-States <=50K
## 2 0 0 13 United-States <=50K
## 3 0 0 40 United-States <=50K
## 4 0 0 40 United-States <=50K
## 5 0 0 40 Cuba <=50K
## 6 0 0 40 United-States <=50K
R has a variety of functions for getting information about directories and files, setting file access permissions, and the like. The following are a few examples:
Used to determine the current working directory.
getwd()
## [1] "C:/Users/pradeep/OneDrive/R Programming 2019-20/programs"
Used to change the current working directory
#setwd(F:/dm,cns,cp and JKC/R PROGRAMMING\stuff by kalipradeep\r files)
Gives file size, creation time, directory-versus-ordinary file status, and so on for each file whose name is in the argument, a character vector.
head(file.info(list.files('C:/Users/pradeep')))
## size isdir mode mtime ctime atime exe
## 12many.tar.lzma NA NA <NA> <NA> <NA> <NA> <NA>
## 3D Objects NA NA <NA> <NA> <NA> <NA> <NA>
## a0poster.tar.lzma NA NA <NA> <NA> <NA> <NA> <NA>
## a2ping.tar.lzma NA NA <NA> <NA> <NA> <NA> <NA>
## a4wide.tar.lzma NA NA <NA> <NA> <NA> <NA> <NA>
## a5comb.tar.lzma NA NA <NA> <NA> <NA> <NA> <NA>
Returns a character vector listing the names of all the files in the directory specified in its first argument.
dir()
## [1] "abc.txt"
## [2] "Basic Math.Rmd"
## [3] "Basic_Math.docx"
## [4] "Basic_Math.html"
## [5] "Basic_Math.tex"
## [6] "classes in r.Rmd"
## [7] "classes_in_r.html"
## [8] "Data types in R part 2-Vectors part 2.Rmd"
## [9] "Data_types_in_R_part_2-Vectors_part_2.html"
## [10] "dataframes.html"
## [11] "dataframes.Rmd"
## [12] "def.txt"
## [13] "factors.rmd"
## [14] "input_and_outputs.html"
## [15] "input_and_outputs.Rmd"
## [16] "kds.txt"
## [17] "lists.rmd"
## [18] "matrices and arrays.Rmd"
## [19] "matrices_and_arrays.html"
## [20] "programs in r.Rmd"
## [21] "programs_in_r.html"
## [22] "questions from unit 1.Rmd"
## [23] "questions_from_unit_1.html"
## [24] "Quicksort.html"
## [25] "Quicksort.Rmd"
## [26] "rsconnect"
## [27] "unit 2-figure"
## [28] "unit 2.md"
## [29] "unit 2.Rpres"
## [30] "vectors part 2.Rmd"
## [31] "vectors part 3.Rmd"
## [32] "vectors_part_2.html"
## [33] "vectors_part_3.html"
Returns a Boolean vector indicating whether the given file exists for each name in the first argument, a character vector.
file.exists("Basic_Math.docx")
## [1] TRUE