R: Read File

From OnnoWiki
Jump to navigation Jump to search

Sumber: http://www.sthda.com/english/wiki/reading-data-from-txt-csv-files-r-base-functions

Fungsi Untuk Import File

  • read.csv(): for reading “comma separated value” files (“.csv”).
  • read.csv2(): variant used in countries that use a comma “,” as decimal point and a semicolon “;” as field separators.
  • read.delim(): for reading “tab-separated value” files (“.txt”). By default, point (“.”) is used as decimal points.
  • read.delim2(): for reading “tab-separated value” files (“.txt”). By default, comma (“,”) is used as decimal points.
# Read tabular data into R
read.table(file, header = FALSE, sep = "", dec = ".")
# Read "comma separated value" files (".csv")
read.csv(file, header = TRUE, sep = ",", dec = ".", ...)
# Or use read.csv2: variant used in countries that 
# use a comma as decimal point and a semicolon as field separator.
read.csv2(file, header = TRUE, sep = ";", dec = ",", ...)
# Read TAB delimited files
read.delim(file, header = TRUE, sep = "\t", dec = ".", ...)
read.delim2(file, header = TRUE, sep = "\t", dec = ",", ...)

Reading a local file

  • file: the path to the file containing the data to be imported into R.
  • sep: the field separator character. “\t” is used for tab-delimited file.
  • header: logical value. If TRUE, read.table() assumes that your file has a header row, so row 1 is the name of each column. If that’s not the case, you can add the argument header = FALSE.
  • dec: the character used in the file for decimal points.
# Read a txt file
my_data <- read.delim(file.choose())
# Read a csv file
my_data <- read.csv(file.choose())
my_data <- read.delim(file.choose(), 
                      stringsAsFactor = FALSE)
my_data <- read.table(file.choose(), 
                      sep ="|", header = TRUE, dec =".")


Reading a file from internet

my_data <- read.delim("http://www.sthda.com/upload/boxplot_format.txt")
head(my_data)




Referensi


Pranala Menarik