Sie sind auf Seite 1von 6

##Directory related

getwd()

setwd("folder path")

list.files()

print (v)

print(class(v))

#shortcuts

#ctrl+shift+c all selected lines will turn comments

#Data Types

# A.1 Vectors with single element

vlogical=TRUE

Vlogical<- TRUE

Vnumeric<- 32.5

vinteger <- 32L

vcomplex <- 2+5i

vcharacter <- 'a'

vcharacter <- "a"

# A.2 vectors with more than 1 element : Use c() or create function.

vlogicalmulti= c(TRUE, FALSE)

vcharactermulti= c("TRUE", "FALSE")

vcharacttermulti2=c("2+4i","2+8i")

vnumericmulti= c(32.5, 89, 78.356)


vintegermulti=c(32.5L,89L)

vcomplexmulti=c(2+4i,2+8i)

#B LISTS

list1 <- list (c(1:5), 35L, 35, "TRUE", FALSE)

print(list1)

#or

v1=c(1:5) #or even v1=1:5

v2=35L

v3=35

v4="TRUE"

v5=FALSE

list2 <- list(v1,v2,v3,v4,v5)

# C MATRICES

m= matrix(c(1,2,3,4,5,6), nrow =3, ncol = 3)

m= matrix(c(1,2,3,4,5,6), nrow =3, ncol = 2)

m= matrix(c(1,2,3,4,5,6), nrow =3, ncol = 3)

m= matrix(c(1,2,3,4,5,6), nrow =3, ncol = 3, byrow=TRUE)


m[2,3]

# D ARRAY

A= array(c('green','yeloow'), dim= c(4,4,3))

A= array(c('green','yeloow', 'red'), dim= c(4,4,3))

A= array(c('green','yeloow', 'red', 'blue', 'violet', 'white', 'black', 1,35L), dim= c(4,4,3))

# E FACTORS

> sex <- c("male", "female", "female", "male", "male", "female", "female")

> sex [1] "male" "female" "female" "male" "male" "female" "female"

You can declare these variables to be factor variables for regression analyses:

> sex.factor<- as.factor(sex)

> sex.factor [1] male female female male male female female

Levels: female male

## LOGICAL OPERATOR

vec1= c(1:10)

vec1>3

vec1[vec1>3]
## Variable assignment

var1= c(30576, "Napean Sea Road Branch")

var2= "transferred from Porbandar"

#printing using cat()

cat("Vivek's PF No is: ", var1, ". He is pres", var2)

cat("Vivek's PF No is: ", var1[1], ". He is presently posted in ", var1[2])

##can we change data type of a variable during a program in R

var_x <- "Hello"

cat("The class of var_x is ",class(var_x),"\n")

var_x <- 34.5

cat(" Now the class of var_x is ",class(var_x),"\n")

var_x <- 27L

cat(" Next the class of var_x becomes ",class(var_x),"\n")

##To know all the variables currently available in the workspace we use the ls() function.

##Also the ls() function can use patterns to match the variable names.

print(ls())

# List the variables starting with the pattern "var".

print(ls(pattern = "var"))

##The variables starting with dot(.) are hidden, they can be listed using "all.names = TRUE"

##argument to ls() function.

print(ls(all.name = TRUE))

##Variables can be deleted by using the rm() function. Below we delete the variable var.3.
##On printing the value of the variable error is thrown.

rm(var.3)

print(var.3)

##When we execute the above code, it produces the following result ???

# [1] "var.3"

#Error in print(var.3) : object 'var.3' not found

#All the variables can be deleted by using the rm() and ls() function together.

rm(list = ls())

print(ls())

## Arithmetic operations

2+2

a=2

b=2

c=a+b

##FUNCTIONS

#1. duplicated() returns dulicates in a array, matrix, vector; return logical answers

vec1=c(1,2,3,4,3,2,3,4)

duplicated(vec1)
MY R2

Das könnte Ihnen auch gefallen