[go: up one dir, main page]

0% found this document useful (0 votes)
6 views22 pages

Unit 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 22

Data Analytics through R

Unit-1

Introduction to R- Programming
 R is a programming language and software environment for statistical analysis and
graphics representation.

 R was created by Ross Ihaka and Robert Gentleman at the University of Auckland,
NewZealand, and is currently developed by the R Development Core Team.
 R is freely available under the GNU General Public License.
 This programming language was named R,based on the first letter of first name of the two R
authors (Robert Gentleman and Ross Ihaka.
 R is the most popular data analytics tool as it is open-source, flexible, offers multiple packages
and has a huge community.
Why R?
R is a programming and statistical language.
R is used for data Analysis and Visualization.
R is simple and easy to learn, read and write.
R is an example of a FLOSS (Free Libre and Open Source Software) where one can freely
distribute copies of this software, read its source code, modify it, etc.

Who uses R?
 The Consumer Financial Protection Bureau uses R for data analysis
 Statisticians at John Deere use R for time series modeling and geospatial analysis in a
reliable and reproducible way.
 Bank of America uses R for reporting.
 R is part of technology stack behind Foursquare’s famed recommendation engine.
 ANZ, the fourth largest bank in Australia, using R for credit risk analysis.
 Google uses R to predict Economic Activity.
 Mozilla, the foundation responsible for the Firefox web browser, uses R to visualize Web
activity.

Evolution of R
 R is an implementation of S programming language which was created by John
Chambers at Bell Labs.
 R was initially written by Ross Ihaka and Robert Gentleman at the Department of
Statistics of the University of Auckland in Auckland, New Zealand.

1
Dr.V.N.Gopiraju, CIET, LAM
Data Analytics through R
 R made its first public appearance in 1993.
 A large group of individuals has contributed to R by sending code and bug reports.Since
mid-1997 there has been a core group (the "R Core Team") who can modify the R source
code archive.
 In the year 2000 R 1.0.0 released.
 R 3.0.0 was released in 2013.

Features of R:
 R supports procedural programming with functions and object-oriented
programming with generic functions. Procedural programming includes procedure,
records, modules, and procedure calls. While object-oriented programming language
includes class, objects, and functions.
 Packages are part of R programming. Hence, they are useful in collecting sets of R
functions into a single unit.
 R is a well-developed, simple and effective programming language which includes
conditionals, loops, user defined recursive functions and input and output facilities.
 R has an effective data handling and storage facility,
 R provides a suite of operators for calculations on arrays, lists, vectors and matrices.
 R provides a large, coherent and integrated collection of tools for data analysis. It
provides graphical facilities for data analysis and display either directly at the computer
or printing at the papers.
 Rs programming features include database input, exporting data, viewing data, variable
labels, missing data, etc.
 R is an interpreted language. So we can access it through command line interpreter.
 R supports matrix arithmetic.
 R, SAS, and SPSS are three statistical languages. Of these three statistical languages, R
is the only an open source.

As a conclusion, R is world’s most widely used statistics programming language. It is a


good choice of data scientists and supported by a vibrant and talented community of
contributors.
HOW to RUN R?
R-Programming can be run in 2 modes
a) Interactive mode: it runs the commands with user interaction
b) Batch Mode: it executes the commands directly without any user interaction.

2
Dr.V.N.Gopiraju, CIET, LAM
Data Analytics through R

Downloading and Installing R


• R is free available from the comprehensive R Archive Network (CRAN) at
http://cran.r-project.org
• Precompiled binaries are available for Linux, Mac OS X and windows.
• R latest release R-3.4.0
• Installing R on windows and Mac is just like installing any other program.
• Install R Studio: a free IDE for R at http://www.rstudio.com/
• If we install R and R Studio, then we need to run R Studio only.
• R is case-sensitive.
• R scripts are simply text files with a .R extension.

3
Dr.V.N.Gopiraju, CIET, LAM
Data Analytics through R

Run R Programming on Your Computer


You will find the easiest way to run R programming on your system (Windows) in this
section.
Run R Programming in Windows
1. Go to official site of R programming(https://www.r-project.org/)
2. Click on the CRAN link on the left sidebar
3. Select a mirror
4. Click "Download R for Windows”
5. Click on the link that downloads the base distribution
6. Run the file and follow the steps in the instructions to install R.

Should I install the 32-bit version or the 64-bit version?


Most people don’t need to worry about this. Obviously the 64-bit version of R won’t work
on a 32-bit machine but both the 32-bit and 64-bit versions of R runs seamlessly on 64-bit
Windows. You might want to consider installing 32-bit version of R if your production
environment is 32-bit because some packages might have compatibility issues and might cause
the “But it works on my machine” fiasco.
Getting help in R
To get help on specific topics, we can use the help() function along with the topic we want
to search. We can also use the ? operator for this. > help(Syntax) > ?Syntax
We also have the help.search() function to do a search engine type of search. We could use
4
Dr.V.N.Gopiraju, CIET, LAM
Data Analytics through R
the ?? operator for this.
> help.search("histograms") > ??"histograms" You must be itching to start learning R by
now. Our collection of R tutorials will help you learn R. Whether you are a beginner or an
expert, each tutorial explains the relevant concepts and syntax with easy-to-understand examples

The prominent editors available for R programming language are:


 RGUI(R graphical user interface) - Rstudio – Studio R offers a richer editing environment
than RGUI and makes some common tasks easier and more fun.
 RStudio - RStudio is an integrated development environment (IDE) for R language. RStudio
is a code editor and development environment, with some nice features that make code
development in R easy and fun.
 R Command Prompt
Once you have R environment setup, then it’s easy to start your R command prompt by just
clicking on R Software icon. This will launch R interpreter and you will get a prompt > where

you can start typing your programs or commands.


>x=6
> print(x)

Usually, you will do your programming by writing your programs in script files and then you
execute those scripts at your command prompt with the help of R interpreter called Rscript. So
let's start with writing following code in a text file called test.R as under –
# My first program in R Programming
myString<- "Hello, World!"

print ( myString)

Save the above code in a file test.R. Execute by opening that script in R editor, select all (Ctrl
+A) and click on run line or selection (Ctrl+R) option in Edit menu of R console.
When we run the above program, it produces the following result.
[1] "Hello, World!"

It is Very Important to understand because these are the objects you will manipulate on a day-to-
day basis in R. Dealing with object conversions is one of the most common sources of frustration
for beginners.

To understand computations in R, two slogans are helpful:

 Everything that exists is an object.


 Everything that happens is a function call.

5
Dr.V.N.Gopiraju, CIET, LAM
Data Analytics through R

Variables, Data types in R:


Everything in R is an object.R has 6 atomic vector types.

 character
 numeric (real or decimal)
 integer
 logical
 complex

By atomic, we mean the vector only holds data of a single type.

 character: "a", "swc"


 numeric: 2, 15.5
 integer: 2L (the L tells R to store this as an integer)
 logical: TRUE, FALSE
 complex: 1+4i (complex numbers with real and imaginary parts)
R provides many functions to examine features of vectors and other objects, for example

 class() - what kind of object is it (high-level)?


 typeof() - what is the object’s data type (low-level)?
 length() - how long is it? What about two dimensional objects?
 attributes() - does it have any metadata?

Basics types of data


 4.5 is a decimal value called numeric.
 4 is a natural value called integers. Integers are also numeric.
 TRUE or FALSE is a Boolean value called logical.
 The value inside " " or ' ' are text (string). They are called characters.
 3+4i is a complex type of data.

Data Objects in R:
Data types are used to store information. In R, we do not need to declare a variable as some data
type. The variables are assigned with R-Objects and the data type of the R-object becomes the
data type of the variable.There are mainly six data types present in R:
1. Vectors
2. Lists
3. Matrices
4. Arrays
5. Factors
6. Data Frames

6
Dr.V.N.Gopiraju, CIET, LAM
Data Analytics through R
Scalar: Scalar variable A scalar is a single number. The following code creates. a scalar variable
with the numeric value 5: x = 5. Vector variable A vector is a sequence of numbers.
1. Vector: A Vector is a sequence of data elements of the same basic type.
Example 1:
>vtr = c(1, 3, 5 ,7 9) or >vtr<- c (1, 3, 5 ,7 9)
>print(vtr)
o/p: [1] 1 3 5 7 9

Example 2:creating sequence vector by usingcolon operator.

>v = 2:12
> print(v)
o/p: [1] 2 3 4 5 6 7 8 9 10 11 12

> v = 3.5:10.5
>v
o/p: [1] 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5

Example 3: If the final element specified does not belong to the sequence then it is discarded.
> v <- 3.8:11
>v
[1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8

Example 4: using seq()(sequence) function, create vector from 1 to9 increment by 2.


>a=seq(1,10,by=2)
>a
o/p: [1] 1 3 5 7 9

Example 4:Accessing vector elements by its position (index).

> day = c("Mon","Tue","Wed","Thurs","Fri","Sat","sun")


> print(day[3])
o/p: [1] "Wed"
> weekend=day[c(6,7)]
> weekend
o/p: [1] "Sat" "sun".
> print(day[c(-2,-3)]) // Negative indexing
o/p: [1] "Mon" "Thurs" "Fri" "Sat" "sun"

There are 5 Atomic vectors, also termed as five classes of vectors.


o/p: [1] "numeric"

3. # Atomic vector of type integer.


Ex: v = 2L
print(class(v))
o/p: [1] "integer"

4. # Atomic vector of type logical.


Ex: v = TRUE
print(class(v))
7
Dr.V.N.Gopiraju, CIET, LAM
Data Analytics through R
o/p: [1] "logical"

8
Dr.V.N.Gopiraju, CIET, LAM
Data analytics Using
R

1. # Atomic vector of type character. Ex: v = "TRUE"


print(class(v))
o/pt: [1] "character"

2. #Atomic vector of type numeric.


Ex: v = 23.5
print(class(v))
5. # Atomic vector of type complex.
Ex: v = 2+5i
print(class(v))
o/p: [1] "complex"

2. List: Lists are the R objects which contain elements of different types like − numbers,
strings, vectors and another list inside it.A list can also contain a matrix or a function as its
elements. List is created using list() function.
Example 1:
>n = c(2, 3, 5)
>s = c("aa", "bb", "cc", "dd", "ee")
>x = list(n, s, TRUE)
>x
O/p –
[[1]]
[1] 2 3 5
[[2]]
[1] "aa" "bb" "cc" "dd" "ee"
[[3]]
[1] TRUE

Example 2: Accessing the elements of list


>mylist = list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3))
> print(mylist[1])
[[1]]
o/p: [1] "Jan" "Feb" "Mar"
> print(mylist[c(1,3)])
o/p: [[1]]
[1] "Jan" "Feb" "Mar"

[[2]]
[[2]][[1]]
[1] "green"

[[2]][[2]]
[1] 12.3

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R
3. Matrices are the R objects in which the elements are arranged in a two-dimensional
rectangular layout. A Matrix is created using the matrix() function.
Example: matrix (data, nrow, ncol, byrow, dimnames) where,
 data is the input vector which becomes the data elements of the matrix.
 nrow is the number of rows to be created.
 ncol is the number of columns to be created.
 byrow is a logical clue. If TRUE then the input vector elements are arranged by row.

 dimname is the names assigned to the rows and columns.


Examples 1:>Mat = matrix(c(1:16), nrow = 4, ncol = 4 )

>Mat
Output :
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
Examples 2:using byrow=TRUE/FALSE
# Create a matrix.
>M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
>print(M)
o/p: [,1] [,2] [,3]
[1,] "a" "a" "b"
[2,] "c" "b" "a"

> M = matrix( c('a','a','b','c','b','a'),nrow=3,byrow=FALSE)


>M
[,1] [,2]
[1,] "a" "c"
[2,] "a" "b"
[3,] "b" "a"

Examples 3:Accessing the elements of matrix


> print(Mat[1,3]) to access 1st row third element.
o/p: [1] 9
> print(Mat[,3]) to access 3rd column
o/p: [1] 9 10 11 12
> print(Mat[1,]) to access 1st row
o/p: [1] 1 5 9 13

Examples 4:giving row names, column names of a matrix


>rownames = c("row1", "row2", "row3", "row4")
>colnames = c("col1", "col2", "col3")
Dr.V.N.Gopiraju, CIET, LAM
Data analytics Using
R> P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
>P
o/p: col1 col2 col3
row1 3 4 5
row2 6 7 8
row3 9 10 11
row4 12 13 14

> P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames =list(c('a','b','c','d'),c(1,2,3)))


>P
o/p: 1 2 3
a 3 45
b 6 7 8
c 9 10 11

d 12 13 14

> P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames =list(1:4,5:7))


>P
o/p: 5 6 7

1 3 4 5

2 6 7 8

3 9 10 11

4 12 13 14

4. Arrays: Arrays are the R data objects which can store data in more than two
dimensions.For example − If we create an array of dimension (2, 3, 4) then it creates 4
rectangular matrices each with 2 rows and 3 columns.While matrices areconfined to two
dimensions, arrays can be of any number of dimensions.An array is created using
the array() function. It takes vectors as input and uses the values in the dim parameter to
create an array. In the below example we create 2 arrays of which are 3x3 matrices each.
Examples 1: Here we create two arrays with two elements which are 3x3 matrices each
>v1 <- c(5,9,3)
>v2 <- c(10,11,12,13,14,15)
>result<- array(c(v1,v2),dim = c(3,3,2))
>result
Output –
,,1
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R, , 2
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
Examples 2.
>a <- array(c('green','yellow'),dim = c(3,3,2))
>print(a)
o/p: , , 1

[,1] [,2] [,3]


[1,] "green" "yellow" "green"
[2,] "yellow" "green" "yellow"
[3,] "green" "yellow" "green"

,,2
[,1] [,2] [,3]
[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"

5. Factors: Factors are the data objects which are used to categorize the data and store it as
levels. They can store both strings and integers. They are useful in data analysis for
statistical modeling.
Factors are created using the factor() function. The nlevels functions gives the count of levels.

# Create a vector.
apple_colors<- c('green','green','yellow','red','red','red','green')

# Create a factor object.


factor_apple<- factor(apple_colors)

# Print the factor.


print(factor_apple)
print(nlevels(factor_apple))
When we execute the above code, it produces the following result −
Dr.V.N.Gopiraju, CIET, LAM
Data analytics Using
R[1] green green yellow red redred green
Levels: green red yellow
[1] 3
Ex2:
>data <- c("East","West","East","North","North","East","West","West“,"East“)
>factor_data<- factor(data)
>factor_data
Output :
[1] East West East North North East West West East
Levels: East North West

6. Data Frames: A data frame is a table or a two-dimensional array-like structure in which


each column contains values of one variable and each row contains one set of values from
each column.Data frames are tabular data objects. Unlike a matrix in data frame each
column can contain different modes of data. The first column can be numeric while the
second column can be character and third column can be logical. It is a list of vectors of
equal length.Data Frames are created using the data.frame() function.
# Create the data frame.
BMI <- data.frame(
gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26)
)
print(BMI)
When we execute the above code, it produces the following result −
gender height weight Age
1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26
Ex2:
>std_id = c (1:5)
>std_name = c("Rick","Dan","Michelle","Ryan","Gary")
>marks = c(623.3,515.2,611.0,729.0,843.25)
Dr.V.N.Gopiraju, CIET, LAM
Data analytics Using
R>std.data<- data.frame(std_id, std_name, marks)
>std.data

Output :
std_id std_name marks

1 1 Rick 623.30
2 2 Dan 515.20
3 3 Michelle 611.00
4 4 Ryan 729.00
5 5 Gary 843.25
By this, we come to the end of different data types in R. Next, let us move forward in R
Tutorial blog and understand another key concept – flow control statements.

Variables: A variable provides us with named storage that our programs can manipulate. A
variable in R can store an atomic vector, group of atomic vectors or a combination of many R-
objects. A valid variable name consists of letters, numbers and the dot or underline characters.
The variable name starts with a letter or the dot not followed by a number.
Variable Name Validity Reason
var_name2. valid Has letters, numbers, dot and underscore
var_name% Invalid Has the character '%'. Only dot(.) and underscore allowed.
2var_name invalid Starts with a number
.var_name , Can start with a dot(.) but the dot(.)should not be followed
valid
var.name by a number.
.2var_name invalid The starting dot is followed by a number making it invalid.
_var_name invalid Starts with _ which is not valid

Variable Assignment
The variables can be assigned values using leftward, rightward and equal to operator. The
values of the variables can be printed using print() or cat()function. The cat() function
combines multiple items into a continuous print output.
# Assignment using equal
operator. var.1 = c(0,1,2,3)

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R
# Assignment using leftward operator.
var.2 <- c("learn","R")

# Assignment using rightward operator.


c(TRUE,1) -> var.3

print(var.1)
cat ("var.1 is ", var.1 ,"\n")
cat ("var.2 is ", var.2 ,"\n")
cat ("var.3 is ", var.3 ,"\n")
When we execute the above code, it produces the following result −
[1] 0 1 2 3
var.1 is 0 1 2 3
var.2 is learn R
var.3 is 1 1
Note − The vector c(TRUE,1) has a mix of logical and numeric class. So logical class is
coerced to numeric class making TRUE as 1.

Data Type of a Variable


In R, a variable itself is not declared of any data type, rather it gets the data type of the R -
object assigned to it. So R is called a dynamically typed language, which means that we can
change a variable’s data type of the same variable again and again when using it in a program.
var_x<- "Hello"
cat("The class of var_x is ",class(var_x),"\n")
class(var_x)
var_x<- 34.5
cat(" Now the class of var_x is ",class(var_x),"\n")
class(var_x)
var_x<- 27L
cat(" Next the class of var_x becomes ",class(var_x),"\n")
class(var_x)

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R
Finding Variables
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())
When we execute the above code, it produces the following result −
[1] "myvar" "my_new_var" "my_var" "var.1"
[5] "var.2" "var.3" "var.name" "var_name2."
[9] "var_x" "varname"
The ls() function can use patterns to match the variable names.
# List the variables starting with the pattern "var".
print(ls(pattern = "var"))
When we execute the above code, it produces the following result −
[1] "myvar" "my_new_var" "my_var" "var.1"
[5] "var.2" "var.3" "var.name" "var_name2."
[9] "var_x" "varname"

Deleting Variables
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())
When we execute the above code, it produces the following result −
character(0)

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R

Operators:

An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. R language is rich in built-in operators and provides following types of
operators.
Types of Operators
We have the following types of operators in R programming −
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Assignment Operators
 Miscellaneous Operators

Arithmetic Operators:
Following table shows the arithmetic operators supported by R language. The operators act on
each element of the vector.

Operator Description Example

+ Adds two vectors


v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)

it produces the following result −

[1] 10.0 8.5 10.0

− Subtracts second vector from


the first v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v-t)

it produces the following result −

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R

[1] -6.0 2.5 2.0

* Multiplies both vectors


v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)

it produces the following result −

[1] 16.0 16.5 24.0

/ Divide the first vector with the


second v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)

When we execute the above code, it


produces the following result −

[1] 0.250000 1.833333 1.500000

%% Give the remainder of the first


vector with the second v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%%t)

it produces the following result −

[1] 2.0 2.5 2.0

%/% The result of division of first


vector with second (quotient) v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R

it produces the following result −

[1] 0 1 1

^ The first vector raised to the


exponent of second vector v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v^t)

it produces the following result −

[1] 256.000 166.375 1296.000

Attach() and detach() functions

The database is attached to the R search path. This means that the database is searched
by R when evaluating a variable, so objects in the database can be accessed by simply giving
their names.
attach() function makes the data available to the R Search Path.
Syntax:
attach(what, pos = 2L, name = deparse(substitute(what), backtick=FALSE),
warn.conflicts = TRUE)
Arguments
what
‘database’. This can be a data.frame or a list or a R data file created
with save or NULL or an environment. See also ‘Details’.
pos
integer specifying position in search() where to attach.
name
name to use for the attached database. Names starting with package: are reserved
for library.
warn.conflicts
logical. If TRUE, warnings are printed about conflicts from attaching the database, unless
that database contains an object .conflicts.OK. A conflict is a function masking a
function, or a non-function masking a non-function.
Details

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R hen evaluating a variable or function name R searches for that name in the databases listed
W
by search. The first name of the appropriate type is used.

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R

By attaching a data frame (or list) to the search path it is possible to refer to the variables in the
data frame by their names alone, rather than as components of the data frame (e.g., in the
example below, height rather than women$height).
By default the database is attached in position 2 in the search path, immediately after the user's
workspace and before all previously attached packages and previously attached databases. This
can be altered to attach later in the search path with the pos option, but you cannot attach at pos =
1.

attach(x)
x: dataframe, matrix, list

Following file has been used for ANOVA analysis:


Subtype,Gender,Expression
A,m,-0.54
A,m,-0.8
A,m,-1.03
A,m,-0.41
A,m,-1.31
A,f,-0.66
A,m,-0.43
A,m,1.01
A,f,-1.15
A,m,0.14
A,m,1.42
A,f,-0.3
A,m,-0.16
A,m,0.15
A,m,-0.62
A,m,-0.42
A,f,-0.4
A,m,-0.35
A,m,-0.42

Let first read in the data from the file:

>x <- read.csv("anova.csv",header=T,sep=",")

There are 3 variables, "Expression", "Gender" and "Subtype". We can display the variables by:
>x$Gender
[1] m mmmm f m m f m m f m mmm f m mmmmm f m mm f m mmm f m mmm

Dr.V.N.Gopiraju, CIET, LAM


Data analytics Using
R

[38] m mmmmmmmm f m f m mmmm f m m f m m f m mmm f m mmmmmmm


[75] m m f m mmmm f m mmmmmmmm f m m f m m f m f m m f m m f m m f m
[112] m f m m f m mm f m mm f m f m f fffff m f m f ff m f fff m f m f
[149] m f f m f ffff m f m f f m f f m f f m f ff m f ff m f ff m f f m f
[186] f f m f f m f m m f m f m f f m f ffff m f f m f ff m mm f m mm f f
[223] f ffff m mm f m f f m f ff m f ff m f fff m f m f fff m f ff m
[260] f f m f fffff m f f m f
fffff m f f Levels: f m

We can't use the variable "Gender" in R Search Path:

>gender
Error: object 'Gender' not found

After attach the object "x", "Gender" can be used globally:

>attach(x)
>Gender
[1] m mmmm f m m f m m f m mmm f m mmmmm f m mm f m mmm f m mmm
[38] m mmmmmmmm f m f m mmmm f m m f m m f m mmm f m mmmmmmm
[75] m m f m mmmm f m mmmmmmmm f m m f m m f m f m m f m m f m m f m
[112] m f m m f m mm f m mm f m f m f fffff m f m f ff m f fff m f m f
[149] m f f m f ffff m f m f f m f f m f f m f ff m f ff m f ff m f f m f
[186] f f m f f m f m m f m f m f f m f ffff m f f m f ff m mm f m mm f f
[223] f ffff m mm f m f f m f ff m f ff m f fff m f m f fff m f ff m
[260] f f m f fffff m f f m f
fffff m f f Levels: f m

detach() function reverses the process:

>detach(x)
>Gender
Error: object 'Gender' not found

Dr.V.N.Gopiraju, CIET, LAM

You might also like