-R management

R can read files from various datatype and sources suche as csv, excel, database, from web, twitter, etc

#R functions for objects

Useful functions for dealing with objects

rm(list=ls()) #delete all objectslength(object) 
# number of elements str(object)    
# structure of an object class(object)  
# class or type of an objectnames(object)  
# names of a objectobject     
# prints the objectls()       
# list current objectsrm(object) 
# delete an objectc(object,object,...)       
# combine objects into a vectorcbind(object, object, ...) 
# combine objects as columnsrbind(object, object, ...) 
# combine objects as rows ​

#R data import/export

Importing data into R is fairly easy methode. The important thing that you must know the function for every sources. The most sources from getting data in R come from CSV plain text and Excel.

#1- Import from CSV plain text: read.table()

  • If you haven't set a working directory: copy the file and paste in file directory (PASTE HERE)

  • Export as CSV

Save the dataset as .csv with plain text editor or another program such as excel

#2-Import from Excel:

  • The best ways to read an Excel file is to export it to a csv , but we can do it direct in R

Note: If you haven't set a working directory: copy the file and paste in function read_excel("PASTE HERE")

  • Export as excel.xlsx?????

Going further : R Data Import Tutorial and R Data Import/Export

#R dataset information

How to get information from the dataset

#R missing data

The symbol NA (not available) are represented missing value. And NaN are not a number (e.g., dividing by zero).

Going further for more NaN values

Missing Values test:

Excluding Missing Values from Analyses:

Advanced Handling of Missing Data:

#R date Values

Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates.

Convert strings to dates and opposite:

Todays date and time:

  • format( ) function to print dates.

Symbol

Meaning

Example

%Y

4-digit year

2007

%y

2-digit year

07

%B

unabbreviated month

January

%b

abbreviated month

Jan

%m

month (00-12)

00-12

%A

unabbreviated weekday

Monday

%a

abbreviated weekday

Mon

%d

day as a number (0-31)

01-31

  • An example:

#R create variable

Use operator "<-" or "=" to create variable

Assign a variable:

Categorial variable:

#R operators

Arithmetic Operators:

Operator

Description

+

addition

-

subtraction

*

multiplication

/

division

^ or **

exponentiation

x %% y

modulus (x mod y) 5%%2 is 1

x %/% y

integer division 5%/%2 is 2

Logical Operators:

Operator

Description

==

exactly equal to

!=

not equal to

>

greater than

>=

greater than or equal to

<

less than

<=

less than or equal to

!x

Not x

x | y

x OR y

x & y

x AND y

isTRUE(x)

test if X is TRUE

#R built in functions-X

Numeric Functions:

Function

Description

abs(x)

absolute value

sqrt(x)

square root

log(x)

natural logarithm

log10(x)

common logarithm

exp(x)

e^x

cos(x), sin(x), tan(x)

-

signif(x, digits=n)

signif(3.475, digits=2) is 3.5

round(x, digits=n)

round(3.475, digits=2) is 3.48

trunc(x)

trunc(5.99) is 5

floor(x)

floor(3.475) is 3

ceiling(x)

ceiling(3.475) is 4

Character Functions:

Function

Description

tolower(x)

Lowercase

toupper(x)

Uppercase

substr(x, start=n1, stop=n2)

Extract or replace substrings x <- "abcdef" substr(x, 2, 3) is "bc" substr(x, 2, 4) <- "22222" is "a22def"

strsplit(x, split)

Split the elements strsplit("abc", "") returns 3 element vector "a","b","c"

paste(..., sep="")

#R control structures-X

Last updated