create a list whose first element is 2-element set of names sat “atanu” and “karam”,second element is a set of numbers like 2,3,5,19,2,7,5,5.

2.check the class of second element of the list 3-print the 4th element of the 2nd element of the list. 4-chNGE THE 1st elemnt of the list as factor 5-find out the frequency of each element of the 2nd element of the list.

x<-list(c("atanu", "karam"),c(2,3,5,9,19,2,7,5,5))
x[1]
## [[1]]
## [1] "atanu" "karam"
x[[c(2,4)]]
## [1] 9
x<-list(c("atanu", "karam"),c(2,3,5,9,19,2,7,5,5))
class(x[[2]])
## [1] "numeric"
x<-list(c("atanu", "karam"),c(2,3,5,9,19,2,7,5,5))
y<-as.factor(x[[1]])
y
## [1] atanu karam
## Levels: atanu karam
x<-list(c("atanu", "karam"),c(2,3,5,9,19,2,7,5,5))
table(x[2])
## 
##  2  3  5  7  9 19 
##  2  1  3  1  1  1

missing values

NA is.na
NaN is.nan

x<-c(1,2,3,NA,1,NaN)
x
## [1]   1   2   3  NA   1 NaN

is.na(x) we get true if na is there

is.nan(x) we get true if nan is there

information abt data frame

data frames

combination of frames or lists is data frame

all the lists should be equal and if we have on extra element in a list it is not a data frame

lenth of the frame is the number of rows..colums should be same.

to import csv to Rstudio..we use read.csv

to import xlxs read.table

to create a “data.frame”

 x<-data.frame(name=c("atanu","rekha","sujith"),age=c(24,25,26),sex=c("M","F","M"))
class(x)
## [1] "data.frame"
x
##     name age sex
## 1  atanu  24   M
## 2  rekha  25   F
## 3 sujith  26   M

to import xlxs read.table

str(x)
## 'data.frame':    3 obs. of  3 variables:
##  $ name: Factor w/ 3 levels "atanu","rekha",..: 1 2 3
##  $ age : num  24 25 26
##  $ sex : Factor w/ 2 levels "F","M": 2 1 2
 x<-data.frame(name=c("atanu","rekha","sujith"),age=c(24,25,26),sex=c("M","F","M"))
y<-as.data.frame(x)
y
##     name age sex
## 1  atanu  24   M
## 2  rekha  25   F
## 3 sujith  26   M
 x<-data.frame(name=c("atanu","rekha","sujith"),age=c(24,25,26),sex=c("M","F","M"))
x[,2]
## [1] 24 25 26
as.character(x[,1])
## [1] "atanu"  "rekha"  "sujith"
x<-data.frame(name=c("atanu","rekha","sujith"),age=c(24,25,26),sex=c("M","F","M"))
names(x[,2])
## NULL
class(as.character(x[,1]))
## [1] "character"
x<-data.frame(name=c("atanu","rekha","sujith"),age=c(24,25,26),sex=c("M","F","M"))
x[,1]=as.character(x[,1])
str(x)
## 'data.frame':    3 obs. of  3 variables:
##  $ name: chr  "atanu" "rekha" "sujith"
##  $ age : num  24 25 26
##  $ sex : Factor w/ 2 levels "F","M": 2 1 2
x[,2]=as.integer(x[,2])
str(x)
## 'data.frame':    3 obs. of  3 variables:
##  $ name: chr  "atanu" "rekha" "sujith"
##  $ age : int  24 25 26
##  $ sex : Factor w/ 2 levels "F","M": 2 1 2
x<-data.frame(name=c("atanu","rekha","sujith"),age=c(24,25,26),sex=c("M","F","M"))
x[,1]=as.character(x[,1])
str(x)
## 'data.frame':    3 obs. of  3 variables:
##  $ name: chr  "atanu" "rekha" "sujith"
##  $ age : num  24 25 26
##  $ sex : Factor w/ 2 levels "F","M": 2 1 2
x<-data.frame(name=c("atanu","rekha","sujith"),age=c(24,25,26),sex=c("M","F","M"))
str(x)
## 'data.frame':    3 obs. of  3 variables:
##  $ name: Factor w/ 3 levels "atanu","rekha",..: 1 2 3
##  $ age : num  24 25 26
##  $ sex : Factor w/ 2 levels "F","M": 2 1 2
x[,2]=as.integer(x[,2])
str(x)
## 'data.frame':    3 obs. of  3 variables:
##  $ name: Factor w/ 3 levels "atanu","rekha",..: 1 2 3
##  $ age : int  24 25 26
##  $ sex : Factor w/ 2 levels "F","M": 2 1 2
x[,2]=as.integer(as.character(x[,2]))
str(x)
## 'data.frame':    3 obs. of  3 variables:
##  $ name: Factor w/ 3 levels "atanu","rekha",..: 1 2 3
##  $ age : int  24 25 26
##  $ sex : Factor w/ 2 levels "F","M": 2 1 2
x<-data.frame(name=c("atanu","rekha","sujith"),age=c(24,25,26),sex=c("M","F","M"))
y<-as.matrix(x)
y
##      name     age  sex
## [1,] "atanu"  "24" "M"
## [2,] "rekha"  "25" "F"
## [3,] "sujith" "26" "M"
str(y)
##  chr [1:3, 1:3] "atanu" "rekha" "sujith" "24" "25" "26" ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:3] "name" "age" "sex"
class(y)
## [1] "matrix"
c
## function (..., recursive = FALSE)  .Primitive("c")
x$age
## [1] 24 25 26
str(x)
## 'data.frame':    3 obs. of  3 variables:
##  $ name: Factor w/ 3 levels "atanu","rekha",..: 1 2 3
##  $ age : num  24 25 26
##  $ sex : Factor w/ 2 levels "F","M": 2 1 2
str(x$name)
##  Factor w/ 3 levels "atanu","rekha",..: 1 2 3
str(x$age)
##  num [1:3] 24 25 26
names(x)
## [1] "name" "age"  "sex"
nrow(x)
## [1] 3
ncol(x)
## [1] 3
z<-data.frame(c("atanu","rekha","sujith"),c(24,25,26),c("M","F","M"))
z
##   c..atanu....rekha....sujith.. c.24..25..26. c..M....F....M..
## 1                         atanu            24                M
## 2                         rekha            25                F
## 3                        sujith            26                M
colnames(z)<-c("Name","Age","Sex")
z
##     Name Age Sex
## 1  atanu  24   M
## 2  rekha  25   F
## 3 sujith  26   M

data set

toremove a column

wine1=wine[,-c(1,2,3)] wine2<-wine[-c(1,2,3),] # to view View(wine) wine6<-wine[1:100,] wine6<-wine[1:100,c(2 ,3)] ## to get particular rows and columns # to get summary summary(wine) summary(wine$v1)

iris

table(iris1$species)

SUBSET FUNCTION

iris_setosa<-subset(iris1,Species==“setosa”) table(iris_setosa$species)

iris2<-subset(iris1,petal.length==1.4) str(iris2) iris4<-subset(iris1,Sepal.Width>3.1) iris4<-subset(iris1,Sepal.Width>=3.1) str(iris4) iris_setosa<-subset(iris1,Species==“setosa”) table(iris_setosa$species)

# and condition iris4<-subset(iris1,Species==“Iris-versicolor” & Sepal.Width>=3.4) str(iris4)

or condition

iris5<-subset(iris1,Species==“Iris-versicolor” | Sepal.Width>=3.4) str(iris5)

USING WHICH SUBSET

iris6<-iris1[which(iris1\(Sepal.length==3.4 & iris1\)Species==“setosa”),] str(iris6)

# 6/10/2015

nchar("leela") ## to get number of characters
## [1] 5
substr("statistics",1,5)
## [1] "stati"
x<-c("leela","sneha","sravs")
substr(x,1,4)
## [1] "leel" "sneh" "srav"
z<-c("us","uk")
substr(z,1,5)
## [1] "us" "uk"

to replace

sub(“old”,“new”,stringname)

l<-"curly is the smart one.curly is funny too."
sub("curly","moe",l) ## to replace only one word
## [1] "moe is the smart one.curly is funny too."
gsub("curly","moe",l) ## to replace only all word
## [1] "moe is the smart one.moe is funny too."
x<-"table prints its required arguements.table."
sub("prints","value",x,"its","that",x)
## [1] "table value its required arguements.table."
gsub("prints","value",x)
## [1] "table value its required arguements.table."

you have twosets of strings and you want to generate all combinations from those two sets

use the outer and pastefunctions together to generate amatrix of all possible combinations

paste: to combine two elements

locations<-c("NY","LA","CHI","HOU")
treatments<-c("T1","T2","T3")
outer(locations,treatments,paste,sep="+")
##      [,1]     [,2]     [,3]    
## [1,] "NY+T1"  "NY+T2"  "NY+T3" 
## [2,] "LA+T1"  "LA+T2"  "LA+T3" 
## [3,] "CHI+T1" "CHI+T2" "CHI+T3"
## [4,] "HOU+T1" "HOU+T2" "HOU+T3"
paste("leela","ambati",sep="")
## [1] "leelaambati"
class(paste("leela","ambati",sep=""))
## [1] "character"
paste("leela","ambati",sep="?")
## [1] "leela?ambati"
class(paste("leela","ambati",sep="?"))
## [1] "character"

getting curent date

sys.Date() class(sys.date()) # problm #you have the string implementation of a date,such as “2010-12-31”,and you want to convert that into a Date object #we have to use as.Date #by default as.Date assumes the string looks like yyyy-mm-dd # to handle in other formats we must specify format as per our requirement .use format=“%m/%d/%y.”

as.Date("2015/10/6")
## [1] "2015-10-06"
as.Date("11/10/2020",format="%m/%d/%y")
## [1] "2020-11-10"
as.Date("1/12/2015",format="%d/%m/%y")
## [1] "2020-12-01"
as.Date("12/1/2015",format="%m/%d/%Y")
## [1] "2015-12-01"
class(as.Date("12/1/2015",format="%m/%d/%Y"))
## [1] "Date"

ISOdate(2012,2,29)

as.Date(ISOdate(2012,2,29)) # also possible to add >ISOdatetime(year,month,day,hour,minute,second)

problem given a date object,you want to extract the julian date-which isin R,the number of days since January 1, 1970.

ISOdate(2015,10,6)
## [1] "2015-10-06 12:00:00 GMT"
as.Date(ISOdate(2015,10,6))
## [1] "2015-10-06"
class(as.Date("2015/10/6",format="%Y/%m/%d"))
## [1] "Date"
z<-ISOdate(2015,10,6)
a<-as.Date(ISOdate(2015,10,6))
format(a,"%Y")
## [1] "2015"
class(format(a,"%Y"))
## [1] "character"
as.integer(format(a,"%Y"))
## [1] 2015
z<-ISOdate(2015,15,12)
a<-as.Date(ISOdate(2015,15,12))
format(a,"%Y")
## [1] NA
a<-c(1,1,2,2,2,3,4,9,15)
a
## [1]  1  1  2  2  2  3  4  9 15
hist(a)

data(cars)
View(cars)
plot(cars$speed,cars$dist)

plot(cars,main="the title",xlab="speed",ylab="dist")
grid()
lines(cars)

data("iris")
#iris<-read.csv("C:/R training/iris.csv")
with(iris, plot(iris$Petal.Length,iris$Petal.Width,xlab="Petal.length",ylab="Petal.Width", pch=as.integer(Species)))
f<-factor(iris$Species)  ##represents elemnts to that particular levels
legend(3.5,2.4,as.character(levels(f)),pch=1:length(levels(f))) ## 3.5,2.4 plotting of levels of Species

adult<-read.csv("C:/R training/adult.csv",header = FALSE)
View(adult)
colnames(adult)<-c("age","workclass","fnlwgt","fnlwgt","education","education_num","marital_status","occupation","relationship","race","sex","capital_gain","capital_loss","hours_per_week","native_country")
with(adult,plot(adult$age,adult$race,pch=as.integer(native_country)))
f1<-factor(adult$native_country)
legend(3.5,3.3,as.character(levels(f1)),pch=1:length(levels(f1)))