Input Data in R Language: c() & scan() Function

Introduction to Input Data in R Language

There are many ways to input data in R Language. Here, I will concentrate only on typing data directly at the keyboard using c() and scan() functions, which are very common ways to input data in R language.

Traditional statistical computer software such as Minitab, SPSS, and SAS, etc., are designed to transform rectangular datasets (a dataset whose rows represent the observations and columns represent the variables) into printed reports and graphs. However, R and S languages are designed to transform data objects into other data objects (such as reports, and graphs).

S and R language both support rectangular datasets, in the form of data frames and other data structures. Here we will learn to know about data in R to work efficiently as a statistical data analyst.

Data Input Functions in R

There are many ways to input data in R and S-Plus. Let us learn to type data directly on the keyboard.

Input Data Using c() Function

The best choice is to enter small datasets directly on the keyboard. Remember that it is impractical to enter a large data set directly at the keyboard.

Let us use the c() function to enter the vector of numbers directly as:

x    <- c(1, 3, 5, 7, 9)
char <- c('a', 'b', 'c', 'd')
TF   <- c(TRUE, FALSE)
Input Data in R Language

Note that the character strings can be directly inputted in single or double quotation marks. For example, "a" and 'a' both are equivalent.

Input Data Using Scan() Function in R

It is also very convenient to use the scan() function in R, which prompts with the index of the next entry.  Consider the example,

xyz <- scan()
1: 10 20 30 35
5: 40 35 25
8: 9 100 50
11:
Read 10 items

The number before the colon on each of the inputted lines is the index of the next data entry point (observation) to be entered. Note that entering a blank line terminates the scan() function input behavior.

Click the following links to learn about data entry (import and export internal and external data) in R Language

R FAQS https://rfaqs.com

Online MCQs Test Preparation Website with Answers

Reading and Writing Data in R: A Quick Guide

For dealing with data, one may need “reading and writing data in R”. Therefore, it is important to discuss reading and writing data in R. It is also important to learn about reading and writing data files in the R environment.

Reading and Writing Data in R Language

Reading Data in R

  • read.table(), and read.csv(), for reading tabular data
  • readLines() for reading lines of a text file
  • source() for reading in R code files (inverse of dump)
  • dget() for reading in R code files (inverse of dput)
  • load() for reading in saved workspaces.

Writing Data to File in R

Following are a few functions for writing (exporting) data to files in R Language.

  • write.table() in R, and write.csv() export data to a wider range of file formats including CSV and tab-delimited.
  • writeLines() write text lines to a text-mode connection.
  • dump() takes a vector of names of R objects and produces text representations of the objects on a file (or connection). A dump file can usually be sourced into another R session.
  • dput() writes an ASCII text representation of an R object to a file (or connection) or uses one to recreate the object.
  • save() writes an external representation of R objects to the specified file.

Reading Data Files with read.table()

The read.table() function in R is one of the most commonly used functions for reading data into R. It has a few important arguments.

  • file, the name of a file, or a connection
  • header, logical indicating if the file has a header line
  • sep, a string indicating how the columns are separated
  • colClasses, a character vector indicating the class of each column in the data set
  • nrows, the number of rows in the dataset
  • comment.char, a character string indicating the comment character
  • skip, the number of lines to skip from the beginning
  • stringsAsFactors, should character variables be coded as factors?

read.table() and read.csv() Examples

data <- read.table("foo.txt")
data <- read.table("D:\\datafiles\\mydata.txt")
data <- read.csv("D:\\datafiles\\mydata.csv")

R will automatically skip lines that begin with a symbol # and figure out how many rows there are (and how much memory needs to be allocated). R also figures out what type of variable is in each table column.

Writing Data Files with write.table() in R

A few important arguments are usually used in function write.table() in R.

  • x, the object to be written, typically a data frame
  • file, the name of the file that the data are to be written to
  • sep, the field separator string
  • col.names, a logical value indicating whether the column names of x are to be written along with x, or a character vector of column names to be written
  • row.names, a logical value indicating whether the row names of x are to be written along with x, or a character vector of row names to be written
  • na, the string to use for missing values in the data

write.table() and write.csv() Examples

The following are few examples about saving data in CSV or TXT files.

x <- data.frame(a = 5, b = 10, c = pi)

write.table(x, file = "data.csv", sep = ",")
write.table(x, "c:\\mydata.txt", sep = "\t")
write.csv(x, file = "data.csv")
Rfaqs: Reading and Writing Data in R

Importing and exporting data in R

SPSS Data Analysis