The R Notes - Part 3

February 8, 2020
r

Reading and Writing Data

Reading tabular data - read.table & read.csv. there return a DataFrame. Equivalent for writing is write.table

read.table(
file, 
header = FALSE,                                  # has separator
sep = "",                                        # seperator example ","default is space
quote = "\"'",
dec = ".", 
numerals = c("allow.loss", "warn.loss", "no.loss"),
row.names, 
col.names, 
as.is = !stringsAsFactors,
na.strings = "NA", 
colClasses = NA,                                 # character vector indicating the class
                                                 # of each vector. not required
nrows = -1,                                      # number of rows. not required
skip = 0,                                        # skip from beginneing
check.names = TRUE, 
fill = !blank.lines.skip,
strip.white = FALSE, 
blank.lines.skip = TRUE,
comment.char = "#",                              # comment symbol. anything to the right is                                                   ignored
allowEscapes = FALSE, 
flush = FALSE,
stringsAsFactors = default.stringsAsFactors(),   # encode character variables as factors
fileEncoding = "", 
encoding = "unknown", 
text, 
skipNul = FALSE)

Consider the following data file:

Name, Surname, Age, Rating, Senior
Mark, Brown, 40, 1, TRUE
Colin, Smith, 41, 1, TRUE
Joe, Blogs, 27, 2, FALSE
#Joe2, Blogs, 27, 2, FALSE

Use the following to read,

data<-read.table("info.txt",header = TRUE, 
sep="," , 
colClasses = c("character", "character", "integer",                 "integer", 
"logical")

print(data)
Name   Surname Age Rating Senior
1      Mark      Brown  40      1   TRUE
2     Colin     Smith  41      1   TRUE
3     Joe  Blogs  27      2  FALSE

When Separator is “,” you can use read.csv. Header is always equal to TRUE

ReadLines reads any file and returns a character vector. Equivalent for writing is WriteLines

Source will read R code, also dget. Equivalent for writing is Dump and dput

load & unserialize will read binary objects. Equivalent for writing is save & serialize

The R Notes - Part 2

February 8, 2020
r

The R Notes - Part 1

February 8, 2020
r
comments powered by Disqus
hugo_cms 11 fuzzy 9 python 8 machine_learning 5 cnc 4 type1_fuzzy 4 type2_fuzzy_library 4 r 3 type2_fuzzy 3 excel 2 iot 2 it2fs 2 weiszfeld_algorithm 2 arduino 1 classifier 1 development 1 game 1 javascript 1 learning 1 mathjax 1 maths 1 mxchip 1 pandas 1 random_walk 1 robot 1 roc 1 set 1 tools 1 type_2_fuzzy 1 vscode 1 wsl 1