0. You can open the prob1.txt and prob.csv in windows notepad. The .csv file will use “,” to separate values on each line by default. But the .txt file often use tab or space to separate values.

1. Import .csv(use the prob1.csv in HW2 for example)

Use header = T to indicate the file contains the names of variables as its first line.

You can specify the file directory or use setwd(“file path”) to set the working directory. I will demonstrate these two methods.

#specify the file directory and use header = T
dta <- read.csv("C:/Users/Cheng_wen_sung/Desktop/prob1.csv",header = T)
str(dta)
## 'data.frame':    60 obs. of  2 variables:
##  $ grade        : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ participation: int  19 18 14 16 24 22 11 22 13 17 ...
head(dta)
##   grade participation
## 1     1            19
## 2     1            18
## 3     1            14
## 4     1            16
## 5     1            24
## 6     1            22
#use setwd("file path") to set the working directory
setwd("C:/Users/Cheng_wen_sung/Desktop")#set working directory
getwd()#check whether we succeed or not
## [1] "C:/Users/Cheng_wen_sung/Desktop"
dta <- read.csv("prob1.csv",header = T)
str(dta)
## 'data.frame':    60 obs. of  2 variables:
##  $ grade        : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ participation: int  19 18 14 16 24 22 11 22 13 17 ...
head(dta)
##   grade participation
## 1     1            19
## 2     1            18
## 3     1            14
## 4     1            16
## 5     1            24
## 6     1            22

If we do not set header = T, R default will set header = T in read.csv function. Let set header = F(false) and see what will happen.

dta1 <- read.csv("C:/Users/Cheng_wen_sung/Desktop/prob1.csv",header = F)
str(dta1)
## 'data.frame':    61 obs. of  2 variables:
##  $ V1: Factor w/ 4 levels "1","2","3","grade": 4 1 1 1 1 1 1 1 1 1 ...
##  $ V2: Factor w/ 19 levels "10","11","12",..: 19 10 9 5 7 15 13 2 13 4 ...
head(dta1)
##      V1            V2
## 1 grade participation
## 2     1            19
## 3     1            18
## 4     1            14
## 5     1            16
## 6     1            24

2. Import .txt(use the prob1.txt in HW2 for example)

The other data type we often use in R is .txt file. Use read.table function to import this data file.

Remember to set header = T to indicate the file contains the names of variables as its first line. If you do not set header = T in read.table function, the default will set header = F. You can compare to read.csv function.

dta <- read.table("C:/Users/Cheng_wen_sung/Desktop/prob1.txt",header = T)
str(dta)
## 'data.frame':    60 obs. of  2 variables:
##  $ grade        : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ participation: int  19 18 14 16 24 22 11 22 13 17 ...
head(dta)
##   grade participation
## 1     1            19
## 2     1            18
## 3     1            14
## 4     1            16
## 5     1            24
## 6     1            22