0% found this document useful (0 votes)
1 views19 pages

Topic1 R Introduction Note

This document serves as an introduction to R programming for econometrics, highlighting its advantages over other software like Stata and Matlab. It covers essential topics such as installing R and RStudio, basic programming concepts, data types, and the use of vectors and matrices. Additional resources and references for further learning are also provided.

Uploaded by

hyearciel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views19 pages

Topic1 R Introduction Note

This document serves as an introduction to R programming for econometrics, highlighting its advantages over other software like Stata and Matlab. It covers essential topics such as installing R and RStudio, basic programming concepts, data types, and the use of vectors and matrices. Additional resources and references for further learning are also provided.

Uploaded by

hyearciel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Topic 1: R introduction

PG Econometrics (Computation)

Toshiaki Aizawa

Acknowledgement
• This note is mainly based on the course text book.
– Florian Heiss “Using R for Introductory Econometrics” Createspace Independent Pub, 2016
∗ Free version available on the web:
∗ [Link]

• For reference, these books or webpages are useful:


– Christoph Hanck, Martin Arnold, Alexander Gerber and Martin Schmelzer “Introduction to
Econometrics with R”
∗ [Link]
– Wickham and Grolemund “R for Data Science”
∗ [Link]
∗ Japanese translation also available.
– Christian Kleiber and Achim Zeileis “Applied Econometrics with R”, Springer
∗ Free PDF version may be available
– Rob Kabacoff “Data Visualization with R”
∗ [Link]

• If you prefer reference in Japanese,


– “R tips”
∗ [Link]
∗ This would be the best start for learning R

• RStudio Cheat Sheets


– R basics in easily accessible and laid-out format!
– [Link]

R introduction
Why do we use R?
• Many alternatives:
– Stata, Matlab, Python, etc. . .
• Free software!!
– Stata is expensive.
– Campus-wide licence for Matlab is available.
• Good balance of flexibility and easy-to-use for econometrics
– Stata is easy to use for econometrics, but hard to write your own program.

1
– Matlab is the opposite.
– You can do everything with R, including data construction, regression analysis, and complicated
structural estimation.
• Many users
– Popular in data science.
– Many packages being developed (especially machine learning methods)

Why do we write programming codes?


• Why do we have to learn to code? Why not just use Excel?
• Excel is great at being a spreadsheet. You should learn it. It’s a pretty bad data analysis tool though.
• Learning a programming language is a very important skill.
• R is free and growing in popularity. It is easy to jump to something like Python if needed.

Don’t be scared
• Programming isn’t all that hard.
• You’re just telling the computer what to do.
• The computer will do exactly as you say.

Introduction of R and R studio


• I strongly recommend you to install RStudio in your laptop and bring it to the class.
• Install in the following order
– R: [Link]
– Rstudio: [Link]
• Now open Rstudio.

Rstudio panes
• Console (Left bottom)
• Environment Pane (Right top)
• Browser Pane (Right bottom)
• Source Editor (Left top)
• tip: Ctrl/Cmd + Shift + number will maximize one of these panes

2
Console (Left bottom)
• Typically bottom-left
• This is where you can type in code and have it run immediately
• Or, when you run code from the Source Editor, it will show up here
• It will also show any output or errors

Console Example
• Let’s copy/paste some code in there to run
#Generate 500 heads and tails
toss <- sample(c("Heads","Tails"),size=500,replace=TRUE)
#Calculate the proportion of heads
mean(toss=="Heads")

## [1] 0.496

What we get back


• We can see the code that we’ve run
• We can see the output of that code, if any
• We can see any errors or warnings.
• Remember - errors mean it didn’t work. Warnings mean it maybe didn’t work.
• Just because there’s no error or warning doesn’t mean it DID work! Always think carefully.

Environment pane (Top-right)


• The output of what we’ve done can also be seen in the Environment pane
• Two important tabs: Environment and History

Environment TAB (Top-right)


• Environment tab shows us all the objects we have in memory
• For example, we created the data object, so we can see that in Environment
• It shows us lots of handy information about that object too (we’ll get to that later)
• You can erase everything with that little broom button.

Brouser pane (Bottom-right)


• Lots of handy stuff here!
• Mostly, the outcome of what you do will be seen here

Source pane (Left top)


• You should be working with code FROM THIS SOURCE PANE, not the console!
• Why? Replicability!
• Also, COMMENTS! USE THEM! PLEASE! # lets you write a comment.

Helps
• The RStudio team has developed a number of “cheatsheets” for working with both R and RStudio.
• This particular cheatsheet for Base R will summarize many of the concepts in this document.

3
Getting Help
• In using R as a calculator, we have seen a number of functions:
– sqrt(), exp(), log() and sin().
• To get documentation about a function in R, simply put a question mark in front of the function name
and RStudio will display the documentation, for example:
?log
?sin
?sqrt

Installing Packages
• One of the main strengths of R as an open-source project is its package system.
• To install a package, use the [Link]() function.
– Think of this as buying a recipe book from the store, bringing it home, and putting it on your
shelf.
[Link]("ggplot2")

• Once a package is installed, it must be loaded into your current R session before being used.
– Think of this as taking the book off of the shelf and opening it up to read.
library(ggplot2)

• Once you close R, all the packages are closed and put back on the imaginary shelf.
• The next time you open R, you do not have to install the package again, but you do have to load any
packages you intend to use by invoking library().

R programming preparation
Data Types
R has a number of basic data types.
• Numeric
– Also known as Double.
– The default type when dealing with numbers.
– Examples: 1, 1.0, 42.5
• Logical
– Two possible values: TRUE and FALSE
– You can also use T and F, but this is not recommended.
– NA is also considered logical.
• Character
– Examples: “a”, “Statistics”, “1 plus 2.”

Basic Calculations and Objects


• We will often want to store results of calculations to reuse them later.
– For this, we can work with basic objects.
– An object has a name and a content.
– R is case sensitive, so x and X are different object names.
• The content of an object is assigned using <- .
– In order to assign the value 5 to the object x, type (the spaces are optional) x <- 5
– You can type <- by typing Alt and −.

4
• A new object with the name x is created and has the value 5.
– If there was an object with this name before, its content is overwritten.
– Assigning a value to an object will not produce any output.
– The simplest shortcut for immediately displaying the result is to put the whole expression into
parentheses as in (x <- 5).

# generate object x (no output):


x <- 5

# display x & xˆ2:


x

## [1] 5
xˆ2

## [1] 25
# generate objects y & z with immediate display using ():
(y <- 3)

## [1] 3
(z <- yˆx)

## [1] 243

• A list of all currently defined object names can be obtained using ls().
• The command exists("name") checks whether an object with the name ‘’name’ ’ is defined and returns
either TRUE or FALSE.
• Removing a previously defined object (for example x) from the workspace is done using rm(x).
• All objects are removed with rm(list = ls()).
– This command is often used when you start your new project.

Vectors
• For statistical calculations, we obviously need to work with data sets including many numbers instead
of scalars.
– The simplest way we can collect many numbers is to use a vector.
• To define a vector, we can collect different values using c(value1,value2,...).
– All the operators and functions used above can be used for vectors.
– Then they are applied to each of the elements separately.

# Define a with immediate output through parentheses:


(a <- c(1,2,3,4,5,6))

## [1] 1 2 3 4 5 6
(b <- a+1)

## [1] 2 3 4 5 6 7

5
(c <- a+b)

## [1] 3 5 7 9 11 13
(d <- b*c)

## [1] 6 15 28 45 66 91
sqrt(d)

## [1] 2.449490 3.872983 5.291503 6.708204 8.124038 9.539392

• There are also specific functions to create, manipulate and work with vectors.
# Define vector
(a <- c(7,2,6,9,4,1,3))

## [1] 7 2 6 9 4 1 3
# Basic functions:
sort(a)

## [1] 1 2 3 4 6 7 9

6
length(a)

## [1] 7

min(a)

## [1] 1
max(a)

## [1] 9
sum(a)

## [1] 32
prod(a)

## [1] 9072

# Creating special vectors:


numeric(20)

## [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rep(1,20)

## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
seq(50)

## [1] 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] 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
5:15

## [1] 5 6 7 8 9 10 11 12 13 14 15
seq(4,20,2)

## [1] 4 6 8 10 12 14 16 18 20

Special types of vectors


• The contents of vectors do not need to be numeric.
• A simple example of a different type are character vectors.
• For handling them, the contents simply need to be enclosed in quotation marks:
cities <- c("New York","Los Angeles","Chicago")
cities

## [1] "New York" "Los Angeles" "Chicago"

• Another useful type are logical vectors.


– Each element can only take one of two values: TRUE or FALSE.
• The easiest way to generate them is to state claims which are either true or false.
• It should be noted that internally, FALSE is equal to 0 and TRUE is equal to 1 and we can do
calculations accordingly.

7
# Basic comparisons:
0 == 1

## [1] FALSE
0 < 1

## [1] TRUE
# Logical vectors:
( a <- c(7,2,6,9,4,1,3) )

## [1] 7 2 6 9 4 1 3
( b <- a<3 | a>=6 )

## [1] TRUE TRUE TRUE TRUE FALSE TRUE FALSE

Naming and Indexing Vectors


• The elements of a vector can be named which can increase the readability of the output.
– Given a vector vec and a string vector namevec of the same length, the names are attached to the
vector elements using names(vec) <- namevec.

• If we want to access a single element or a subset from a vector, we can work with indices.
– They are written in square brackets [] next to the vector name.
– For example myvector[4] returns the 4th element of myvector.
– myvector[6] <- 8 changes the 6th element to take the value 8.

• For extracting more than one element, the indices can be provided as a vector themselves.
– If the vector elements have names, we can also use those as indices like in myvector["elementname"].
• Finally, logical vectors can also be used as indices.
– If a general vector vec and a logical vector b have the same length, then vec[b] returns the
elements of vec for which b has the value TRUE.

# Create a vector "avgs":


avgs <- c(0.366, 0.358, 0.356, 0.349, 0.346)

# Create a string vector of names:


players <- c("Cobb","Hornsby","Jackson","O'Doul","Delahanty")

# Assign names to vector and display vector:


names(avgs) <- players
avgs

## Cobb Hornsby Jackson O'Doul Delahanty


## 0.366 0.358 0.356 0.349 0.346

# Indices by number:
avgs[2]

## Hornsby

8
## 0.358
avgs[1:4]

## Cobb Hornsby Jackson O'Doul


## 0.366 0.358 0.356 0.349

# Indices by name:
avgs["Jackson"]

## Jackson
## 0.356
# Logical indices:
avgs[ avgs>=0.35 ]

## Cobb Hornsby Jackson


## 0.366 0.358 0.356

Matrices
• Matrices are important tools for econometric analyses.
• Most often in applied econometrics, matrices will be generated from an existing data set.
1. matrix(vec,nrow=m) takes the numbers stored in vector vec and put them into a matrix with m rows.
2. rbind(r1,r2,...) takes the vectors r1,r2,. . . (which obviously should have the same length) as the
rows of a matrix.
3. cbind(c1,c2,...) takes the vectors c1,c2,. . . (which obviously should have the same length) as the
columns of a matrix.

# Generating matrix A from one vector with all values:


v <- c(2,-4,-1,5,7,0)
( A <- matrix(v,nrow=2) )

## [,1] [,2] [,3]


## [1,] 2 -1 7
## [2,] -4 5 0

# Generating matrix A from two vectors


# corresponding to rows:
row1 <- c(2,-1,7)
row2 <- c(-4,5,0)
( A <- rbind(row1, row2) )

## [,1] [,2] [,3]


## row1 2 -1 7
## row2 -4 5 0

# Generating matrix A from three vectors


# corresponding to columns:
col1 <- c(2,-4)
col2 <- c(-1,5)

9
col3 <- c(7,0)
( A <- cbind(col1, col2, col3) )

## col1 col2 col3


## [1,] 2 -1 7
## [2,] -4 5 0

# Giving names to rows and columns:


colnames(A) <- c("Alpha","Beta","Gamma")
rownames(A) <- c("Aleph","Bet")
A

## Alpha Beta Gamma


## Aleph 2 -1 7
## Bet -4 5 0

• I personally like to create the new matrix like this:


# creating an empty matrix
new_mat <- matrix(NA,nrow=3,ncol=3)
new_mat[,1] <- 1:3 # Filling in the first colum
new_mat[,2] <- 4:6
new_mat[,3] <- 7:9
new_mat # Display the matrix

## [,1] [,2] [,3]


## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9

• We can also create special matrices as the examples in the output show:
1. diag(vec) (where vec is a vector) creates a diagonal matrix with the elements on the main diagonal
given in vector vec.
2. diag(n) (where n is a scalar) creates the n × n identity matrix.
• If instead of a vector or scalar, a matrix M is given as an argument to the function diag, it will return
the main diagonal of M.

# Diaginal and identity matrices:


diag( c(4,2,6) )

## [,1] [,2] [,3]


## [1,] 4 0 0
## [2,] 0 2 0
## [3,] 0 0 6
diag( 3 )

## [,1] [,2] [,3]


## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1

10
• We can give a row and then a column index (or vectors of indices), separated by a comma:
1. A[2,3] is the element in row 2, column 3
2. A[2,c(1,2)] is a vector consisting of the elements in row 2, columns 1 and 2
3. A[2,] is a vector consisting of the elements in row 2, all columns

# Indexing for extracting elements (still using A from above):


A[2,1]

## [1] -4
A[,2]

## Aleph Bet
## -1 5
A[,c(1,3)]

## Alpha Gamma
## Aleph 2 7
## Bet -4 0

Matrix algebra
• Basic matrix algebra includes:
1. Matrix addition using the operator + as long as the matrices have the same dimensions.
2. The operator * does not do matrix multiplication but rather element-wise multiplication.
3. Matrix multiplication is done with the somewhat clumsy operator %*% (yes, it consists of three characters!)
as long as the dimensions of the matrices match.
4. Transpose of a matrix X: as t(X)
5. Inverse of a matrix X: as solve(X)

Element-wise multiplication
A <- matrix( c(2,-4,-1,5,7,0), nrow=2)
B <- matrix( c(2,1,0,3,-1,5), nrow=2)
A

## [,1] [,2] [,3]


## [1,] 2 -1 7
## [2,] -4 5 0
B

## [,1] [,2] [,3]


## [1,] 2 0 -1
## [2,] 1 3 5
A*B

## [,1] [,2] [,3]


## [1,] 4 0 -7
## [2,] -4 15 0

11
Transpose
# Transpose:
(C <- t(B) )

## [,1] [,2]
## [1,] 2 1
## [2,] 0 3
## [3,] -1 5

Multiplication
# Matrix multiplication:
(D <- A %*% C )

## [,1] [,2]
## [1,] -3 34
## [2,] -8 11

Inverse
# Inverse:
solve(D)

## [,1] [,2]
## [1,] 0.0460251 -0.1422594
## [2,] 0.0334728 -0.0125523

Data Frames
Data Frames
• A data frame is an object that collects several variables.
– It is similar to a matrix.
– The most important difference to a matrix is that a data frame can contain variables of different
types (like numerical, logical, string and factor), whereas matrices can only contain numerical
values.
• Like a matrix, the rows can have names.
– Unlike a matrix, the columns always contain names which represent the variables.
– We can define a data frame from scratch by using the command [Link] or [Link]
which transform inputs of different types (like a matrix) into a data frame.

# Define a matrix from vectors:


product1<-c(0,3,6,9,7,8)
product2<-c(1,2,3,5,9,6)
product3<-c(2,4,4,2,3,2)
sales_mat <- cbind(product1,product2,product3)
# The matrix looks like this:
sales_mat

## product1 product2 product3


## [1,] 0 1 2
## [2,] 3 2 4
## [3,] 6 3 4

12
## [4,] 9 5 2
## [5,] 7 9 3
## [6,] 8 6 2

# Create a data frame and display it:


sales <- [Link](sales_mat)
sales

## product1 product2 product3


## 1 0 1 2
## 2 3 2 4
## 3 6 3 4
## 4 9 5 2
## 5 7 9 3
## 6 8 6 2

• The outputs of the matrix sales_mat and the data frame sales look exactly the same, but they behave
differently.
• In RStudio, the difference can be seen in the Workspace window (top right by default).
– It reports the content of sales_mat to be a ‘’6x3 double matrix’ ’ whereas the content of sales is ‘’6
obs. of 3 variables’ ’.

• We can address a single variable var of a data frame df using the matrix-like syntax df[,"var"] or by
stating df$var.
– This can be used for extracting the values of a variable but also for creating new variables.

# Accessing a single variable:


sales$product2

## [1] 1 2 3 5 9 6
# Generating a new variable in the data frame:
sales$totalv1 <- sales$product1 + sales$product2+
sales$product3

# tidyverse style
sales <- sales %>% mutate(totalv1=product1+product2+product3)

# Result:
sales

## product1 product2 product3 totalv1


## 1 0 1 2 3
## 2 3 2 4 9
## 3 6 3 4 13
## 4 9 5 2 16
## 5 7 9 3 19
## 6 8 6 2 16

13
Subset of the data
• Sometimes, we do not want to work with a whole data set but only with a subset.
• This can be easily achieved with the command subset(df,criterion),
– where criterion is a logical expression which evaluates to TRUE for the rows which are to be
selected.

# Full data frame (from Data-frames.R, has to be run first)


sales

## product1 product2 product3 totalv1


## 1 0 1 2 3
## 2 3 2 4 9
## 3 6 3 4 13
## 4 9 5 2 16
## 5 7 9 3 19
## 6 8 6 2 16
# Subset: all years in which sales of product 3 were >=3
subset(sales, product3>=3)

## product1 product2 product3 totalv1


## 2 3 2 4 9
## 3 6 3 4 13
## 5 7 9 3 19
# tidyverse style
sales%>%filter(product3>=3)

## product1 product2 product3 totalv1


## 1 3 2 4 9
## 2 6 3 4 13
## 3 7 9 3 19

Basic Information on a Data Set


• After loading a data set into a data frame, it is often useful to get a quick overview of the variables it
contains.
1. head(df) displays the first few rows of data.
2. str(df) lists the structure, i.e. the variable names, variable types (numeric, string, logical, factor,. . . ),
and the first few values.
3. colMeans(df) reports the averages of all variables and summary(df) shows summary statistics.

Descriptive statistics for discrete variables


• Frequencies
– For discrete variables, the most fundamental statistics are the frequencies of outcomes.
– The command table(x) gives such a table of counts.
• For getting the sample shares instead of the counts, we can request [Link](table(x)).

• As an example, we look at the data set [Link].


1. kids = 1 if the respondent has at least one child
2. ratemarr = Rating of the own marriage (1=very unhappy, 5=very happy)

# load data set


data(affairs, package='wooldridge')

14
# Check the numbers of observations and variables
dim(affairs)

## [1] 601 19

# Frequencies for having kids:


table(affairs$kids)

##
## 0 1
## 171 430

• Marriage ratings:
– 1 “very unhappy”
– 2 “unhappy”
– 3 “average”
– 4 “happy”
– 5 “very happy”
# Marriage ratings (share):
(rate <- table(affairs$ratemarr) )

##
## 1 2 3 4 5
## 16 66 93 194 232
# Marriage ratings (share):
[Link](rate)

##
## 1 2 3 4 5
## 0.0266223 0.1098170 0.1547421 0.3227953 0.3860233

Contingency Tables
• If we provide two arguments like table(x,y), we get the contingency table,
• i.e. the counts of each combination of outcomes for variables x and y.
# contingency table
(countstab <- table(affairs$kids,affairs$ratemarr))

##
## 1 2 3 4 5
## 0 3 8 24 40 96
## 1 13 58 69 154 136

• For the two-way tables, we can get a table of


1. the overall sample share: [Link](table(x,y))
2. the share within x values (row percentages): [Link](table(x,y),margin=1)
3. the share within y values (column percentages): [Link](table(x,y),margin=2)

# overall sample share:


[Link](countstab)

15
##
## 1 2 3 4 5
## 0 0.004991681 0.013311148 0.039933444 0.066555740 0.159733777
## 1 0.021630616 0.096505824 0.114808652 0.256239601 0.226289517

# Share within "marriage" (i.e. within a row):


[Link](countstab, margin=1)

##
## 1 2 3 4 5
## 0 0.01754386 0.04678363 0.14035088 0.23391813 0.56140351
## 1 0.03023256 0.13488372 0.16046512 0.35813953 0.31627907

# Share within "haskids" (i.e. within a column):


[Link](countstab, margin=2)

##
## 1 2 3 4 5
## 0 0.1875000 0.1212121 0.2580645 0.2061856 0.4137931
## 1 0.8125000 0.8787879 0.7419355 0.7938144 0.5862069

Statistics with R
Fundamental Statistics
• The command summary is a generic command that accepts many different object types and reports appropriate
summary information.
– For numerical vectors, summary displays the mean, median, quartiles and extreme values.
• summary(df) shows the summary statistics for all variables.
– To calculate all averages within rows or columns of matrices or data frames, consider the commands
colSums, rowSums, colMeans, and rowMeans.

data(ceosal1, package='wooldridge')
# sample average:
mean(ceosal1$salary)

## [1] 1281.12
# sample median:
median(ceosal1$salary)

## [1] 1039

#standard deviation:
sd(ceosal1$salary)

## [1] 1372.345
# summary information:
summary(ceosal1$salary)

## Min. 1st Qu. Median Mean 3rd Qu. Max.


## 223 736 1039 1281 1407 14822

16
# correlation with ROE:
cor(ceosal1$salary, ceosal1$roe)

## [1] 0.1148417

Practice question 1.1 -Fundamental Statistics


Use the data in [Link] to answer this question.

1. How many women are in the sample, and how many report smoking during pregnancy?
2. What is the average number of cigarettes smoked per day? Is the average a good measure of the ‘’typical’ ’
woman in this case? Explain.
# Road the data set
data(bwght, package='wooldridge')
# Write your answer here

• Hint:
• Check the number of rows by dim(df) or nrow(df).
• Define smokers. smoker <- bwght$cigs>0.
• Count the number of smokers by sum(smoker).
• use mean(df$var) to calculate the mean value of variable.
• Check the distribution of the variable by table(df$var).
– Alternatively, you can visualize the distribution by hist(df$var).

Lists
Lists
• A list is a generic collection of objects.
• Unlike vectors, the components can have different types.
• Each component can (and in the cases relevant for us will) be named.
• Lists can be generated with a command like
mylist <- list( name1=component1, name2=component2, ... )

• The names of the components are returned by names(mylist).


• A component can be addressed by name using mylist$name or using numbers mylist[[2]].

# Generate a list object:


mylist <- list( A=seq(8,36,4), this="that", idm = diag(3))
# Print whole list:
mylist

## $A
## [1] 8 12 16 20 24 28 32 36
##
## $this
## [1] "that"
##
## $idm
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1

17
# Vector of names:
names(mylist)

## [1] "A" "this" "idm"


# Print component "A":
mylist$A

## [1] 8 12 16 20 24 28 32 36
# or
mylist[[1]]

## [1] 8 12 16 20 24 28 32 36

R Data Files
• R has its own data file format.
• The usual extension of the file name is .RData.
– It can contain one or more objects of arbitrary type (scalars, vectors, matrices, data frames, . . . ).
– If the objects v1,v2,. . . are currently in the workspace, they can be saved to a file named [Link] by

save(v1,v2,..., file="[Link]")

• To save all currently defined objects, use save(list=ls(), file="[Link]") instead.


• All objects stored in [Link] can be loaded into the workspace with

load("[Link]")

Import and Export of Data


Import and Export of Text Files
• Probably all software packages that handle data are capable of working with data stored as text files.
• This makes them a natural way to exchange data between different programems and users.
– Common file name extensions for such data files are RAW, CSV or TXT.
• The R command [Link] provides possibilities for reading many flavors of text files which are then stored
as a data frame.

newdataframe <- [Link](filename, ...)

• The optional arguments that can be added, separated by comma, include but are not limited to:

1. header=TRUE: The text file includes the variable names as the first line
2. sep=",": Instead of spaces or tabs, the columns are separated by a comma.

• Instead, an arbitrary other character can be given. sep=";" might be another relevant example of a separator.

1. dec=",": Instead of a decimal point, a decimal comma is used.

• For example, some international versions of MS Excel produce these sorts of text files.

1. [Link]=number: The values in column number are used as row names instead of variables.

• RStudio provides a graphical user interface for importing text files which also allows to preview the effects of
changing the options:
– In the Workspace window, click on ‘’Import Dataset’ ’.

18
• In file [Link], the columns are separated by a comma.
• The correct command for the import would be

mydata <- [Link]("[Link]", sep=",")

or
library(tidyverse)
mydata <- read_csv("[Link]")

• Since this data file does not contain any variable names, they are set to their default values V1 through V4 in
the resulting data frame mydata.
• They can be changed manually afterward, e.g. by

colnames(mydata) <- c("year","prod1","prod2","prod3").

• Given some data in a data frame mydata, they can be exported to a text file using similar options as for
[Link] using

[Link](mydata, file = "myfilename", ...)

• You can make R read the various types of dataset. Some packages such as foreign allows you to read Stata
data set (.dta).

library(foreign)

19

You might also like