1. Accessing the Keyboard and Monitor

R provides several functions for accesssing the keyboard and monitor. Here, we’ll look at the scan(), readline(), print(), and cat() functions.

1.1 Using the scan() Function

You can use scan() to read in a vector, whether numeric or character, from a file or the keyboard.

a<-scan("")
a
## numeric(0)

1.1.1 Scan function to take numeric input from a text file.

scan("C:/Users/pradeep/OneDrive/R Programming 2019-20/programs/def.txt")
## [1] 1 2 3 4 5 6 7 8 9

1.1.2 Scan function to take string input from a text file.

scan("C:/Users/pradeep/OneDrive/R Programming 2019-20/programs/abc.txt",what = "")
## [1] "AB"  "C"   "DEF" "G"   "H"

1.1.3 Scan function by changing the separator.

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"

1.2 Using readline() function.

If you want to read in a single line from the keyboard, readline() is very handy.

w<-readline()
w
## [1] ""

1.2.1 Using readline() function with optional prompt message.

w<-readline("Type your name:")
## Type your name:
w
## [1] ""

1.3 Printing to the screen.

1.3.2 cat() function.

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

2. Reading and writing files.

2.1 read.table for reading a table.

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

2.2 Reading a matrix.

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

3. Introduction to Connections

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.

3.1 Reading from a file.

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

3.2 Reading from a file. All at a time.

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"

3.3 Writing to a 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")

3.4 Creating a matrix and store it in a text file

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)

3.5 The function cat() can also be used to write to a file, one part at a time. Here’s an example:

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

3.6 Connections for writing contents to a file

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)

4 Accessing Files on Remote Machines via URLs

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

5 Getting File and Directory Information

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:

5.1) getwd()

Used to determine the current working directory.

getwd()
## [1] "C:/Users/pradeep/OneDrive/R Programming 2019-20/programs"

5.2) setwd()

Used to change the current working directory

#setwd(F:/dm,cns,cp and JKC/R PROGRAMMING\stuff by kalipradeep\r files)

5.3) file.info

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>

5.4) dir()

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"

5.5) file.exists()

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